Pdf events: on chapter, on chapter end, on section, on document Close Document : Page Event « PDF « Java Tutorial






import java.io.FileOutputStream;

import com.lowagie.text.Chapter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.tools.plugins.Concat;

public class MainClass extends PdfPageEventHelper {
  protected Document toc = new Document();

  public MainClass() throws Exception {
    PdfWriter.getInstance(toc, new FileOutputStream("toc.pdf"));
    toc.open();
  }

  public void onChapter(PdfWriter writer, Document document, float position, Paragraph title) {
    try {
      toc.add(new Paragraph(title.content() + " page " + document.getPageNumber()));
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

  public void onChapterEnd(PdfWriter writer, Document document, float position) {
    try {
      toc.add(Chunk.NEWLINE);
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

  public void onSection(PdfWriter writer, Document document, float position, int depth,
      Paragraph title) {
    try {
      switch (depth) {
      case 2:
        toc.add(new Paragraph(title.content(), new Font(Font.HELVETICA, 10)));
        break;
      default:
        toc.add(new Paragraph(title.content(), new Font(Font.HELVETICA, 8)));
      }
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

  public void onCloseDocument(PdfWriter writer, Document document) {
    toc.close();
  }

  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("chapter_events.pdf"));
    writer.setPageEvent(new MainClass());
    document.open();
    Paragraph hello = new Paragraph("hello");
    Chapter universe = new Chapter("asdf", 1);
    Section section;
    section = universe.addSection("World");
    section.add(hello);
    Section planets = universe.addSection("Planets");
    planets.add(hello);
    section = planets.addSection("to Venus:");
    section.add(hello);
    document.add(universe);
    Chapter people = new Chapter("People", 2);
    section = people.addSection("asasdf");
    section.add(hello);
    document.add(people);
    document.setPageSize(PageSize.A4.rotate());
    Chapter animals = new Chapter("3", 3);
    section = animals.addSection("section");
    section.add(hello);
    document.add(animals);
    document.close();

    String[] arguments = { "toc.pdf", "chapter_events.pdf", "toc_chapters.pdf" };
    Concat.main(arguments);
  }
}








29.22.Page Event
29.22.1.extends PdfPageEventHelper
29.22.2.Pdf document footer
29.22.3.Pdf Page Event Helper
29.22.4.Set PageEvent to Pdf document
29.22.5.Pdf events: on chapter, on chapter end, on section, on document Close Document
29.22.6.Transition and Duration