Using Pdf event to build the Pdf document : Document Page Event « PDF RTF « Java






Using Pdf event to build the Pdf document

Using Pdf event to build the Pdf document
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

public class PdfPageEventHelperDemo extends PdfPageEventHelper {
  public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
    if ("text".equals(text)) {
      PdfContentByte cb = writer.getDirectContent();
      cb.ellipse(rect.left(), rect.bottom() - 5f, rect.right(), rect.top());
      cb.stroke();
    }
  }

  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PdfPageEventHelperDemo.pdf"));
      writer.setPageEvent(new PdfPageEventHelperDemo());
      document.open();

      Paragraph p = new Paragraph("Generic page event");
      document.add(p);
      
      Chunk box = new Chunk("text");
      box.setGenericTag("text");
      
      p.add(box);
      document.add(p);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}
           
       








itext.zip( 1,748 k)

Related examples in the same category

1.PdfEvents: on generic tags, on chapterPdfEvents: on generic tags, on chapter
2.PdfPageEvent: page endPdfPageEvent: page end
3.PdfPageEvent: on ParagraphPdfPageEvent: on Paragraph
4.Document Page Event: document open, close, page start and endDocument Page Event: document open, close, page start and end