Java Tutorial/PDF/Page Event

Материал из Java эксперт
Перейти к: навигация, поиск

extends PdfPageEventHelper

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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 MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setPageEvent(new MyPdfPageEventHelper());
    document.open();
    Paragraph p = new Paragraph();
    Chunk fox = new Chunk("test");
    fox.setGenericTag("box");
    p.add(fox);
    p.add(" this is a test ");
    Chunk dog = new Chunk("test ");
    dog.setGenericTag("ellipse");
    p.add(dog);
    document.add(p);
    document.close();
  }
}
class MyPdfPageEventHelper extends PdfPageEventHelper {
  public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
    if ("ellipse".equals(text)) {
      PdfContentByte cb = writer.getDirectContent();
      cb.setRGBColorStroke(0xFF, 0x00, 0x00);
      cb.ellipse(rect.left(), rect.bottom() - 5f, rect.right(), rect.top());
      cb.stroke();
      cb.resetRGBColorStroke();
    } else if ("box".equals(text)) {
      PdfContentByte cb = writer.getDirectContentUnder();
      rect.setBackgroundColor(new Color(0xa5, 0x2a, 0x2a));
      cb.rectangle(rect);
    }
  }
}





Pdf document footer

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass extends PdfPageEventHelper {
  protected Phrase header = new Phrase("header");
  protected PdfPTable footer = new PdfPTable(4);
  public MainClass() {
    footer.setTotalWidth(300);
    footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    footer.addCell(new Phrase(new Chunk("First").setAction(new PdfAction(PdfAction.FIRSTPAGE))));
    footer.addCell(new Phrase(new Chunk("Prev").setAction(new PdfAction(PdfAction.PREVPAGE))));
    footer.addCell(new Phrase(new Chunk("Next").setAction(new PdfAction(PdfAction.NEXTPAGE))));
    footer.addCell(new Phrase(new Chunk("Last").setAction(new PdfAction(PdfAction.LASTPAGE))));
  }
  public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    if (document.getPageNumber() > 1) {
      ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header, (document.right() - document
          .left())
          / 2 + document.leftMargin(), document.top() + 10, 0);
    }
    footer.writeSelectedRows(0, -1, (document.right() - document.left() - 300) / 2
        + document.leftMargin(), document.bottom() - 10, cb);
  }
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
    writer.setPageEvent(new MainClass());
    document.open();
    for (int k = 1; k <= 300; ++k) {
      document.add(new Phrase("test "));
    }
    document.close();
  }
}





Pdf events: on chapter, on chapter end, on section, on document Close Document

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);
  }
}





Pdf Page Event Helper

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass extends PdfPageEventHelper {
  protected PdfTemplate total;
  protected BaseFont helv;
  public void onOpenDocument(PdfWriter writer, Document document) {
    total = writer.getDirectContent().createTemplate(100, 100);
    total.setBoundingBox(new Rectangle(-20, -20, 100, 100));
    try {
      helv = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    } catch (Exception e) {
      throw new ExceptionConverter(e);
    }
  }
  public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    String text = "Page " + writer.getPageNumber() + " of ";
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    cb.showText(text);
    cb.endText();
    cb.restoreState();
  }
  public void onCloseDocument(PdfWriter writer, Document document) {
    total.beginText();
    total.setFontAndSize(helv, 12);
    total.setTextMatrix(0, 0);
    total.showText(String.valueOf(writer.getPageNumber() - 1));
    total.endText();
  }
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setPageEvent(new MainClass());
    document.open();
    Paragraph p = new Paragraph();
    for (int k = 1; k <= 3000; ++k) {
      p.add(new Phrase("test"));
    }
    p.setAlignment(Element.ALIGN_JUSTIFIED);
    for (int k = 1; k <= 12; ++k) {
      document.add(p);
    }
    document.close();
  }
}





Set PageEvent to Pdf document

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass extends PdfPageEventHelper {
  BaseFont bf;
  public void onOpenDocument(PdfWriter arg0, Document arg1) {
    try {
      bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    } catch (Exception e) {
      bf = null;
    }
  }
  public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    Rectangle pageSize = writer.getPageSize();
    Rectangle art = writer.getBoxSize("bleed");
    cb.rectangle(art.left(), art.bottom(), art.width(), art.height());
    cb.stroke();
    cb.setLineWidth(3);
    cb.restoreState();
  }
  public static void main(String[] args) throws Exception {
    Document document = new Document(new Rectangle(432, 792));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setViewerPreferences(PdfWriter.PageLayoutTwoPageLeft);
    writer.setPageEvent(new MainClass());
    writer.setCropBoxSize(new Rectangle(5, 5, 427, 787));
    writer.setBoxSize("bleed", new Rectangle(30, 30, 402, 762));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    Paragraph p = new Paragraph();
    for (int i = 0; i < 10; i++) {
      p.add(new Phrase("test "));
    }
    for (int i = 0; i < 10; i++) {
      ct.addElement(p);
    }
    int status = ColumnText.NO_MORE_COLUMN;
    while (ColumnText.hasMoreText(status)) {
      ct.setSimpleColumn(72, 72, 360, 684);
      status = ct.go();
      document.newPage();
    }
    document.close();
  }
}





Transition and Duration

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTransition;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass extends PdfPageEventHelper {
  protected PdfTransition transition;
  protected int duration;
  public MainClass(PdfTransition transition, int duration) {
    this.transition = transition;
    this.duration = duration;
  }
  public void setTransition(PdfTransition transition) {
    this.transition = transition;
  }
  public void setDuration(int duration) {
    this.duration = duration;
  }
  public void onStartPage(PdfWriter writer, Document document) {
    writer.setTransition(transition);
    writer.setDuration(duration);
  }
  public static void main(String[] args) throws Exception {
    Document document = new Document(new Rectangle(144, 115));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    writer.setViewerPreferences(PdfWriter.PageModeFullScreen);
    MainClass slideshow = new MainClass(new PdfTransition(PdfTransition.OUTBOX), 1);
    writer.setPageEvent(slideshow);
    document.open();
    Image img0 = Image.getInstance("0.gif");
    img0.setAbsolutePosition(0, 0);
    document.add(img0);
    document.newPage();
    Image img1 = Image.getInstance("1.gif");
    img1.setAbsolutePosition(0, 0);
    document.add(img1);
    document.newPage();
    document.close();
  }
}