Java/PDF RTF/Document Page Event
Содержание
Document Page Event: document open, close, page start and end
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class DocumentPageEventPDF extends PdfPageEventHelper {
public Image headerImage;
public PdfPTable table;
public PdfGState gstate;
public PdfTemplate tpl;
public BaseFont helv;
public static void main(String args[]) {
try {
Document doc = new Document(PageSize.A4, 50, 50, 100, 72);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("DocumentPageEventPDF.pdf"));
writer.setPageEvent(new DocumentPageEventPDF());
doc.open();
Paragraph p = new Paragraph("text");
doc.add(p);
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onOpenDocument(PdfWriter writer, Document document) {
System.out.println("onOpenDocument");
}
public void onEndPage(PdfWriter writer, Document document) {
System.out.println("onEndPage");
}
public void onStartPage(PdfWriter writer, Document document) {
System.out.println("onStartPage");
}
public void onCloseDocument(PdfWriter writer, Document document) {
System.out.println("onCloseDocument");
}
}
PdfEvents: on generic tags, on chapter
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class PdfEvents {
public static void main(String[] args) {
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PdfEvents.pdf"));
document.open();
writer.setPageEvent(new MyPageEvents());
document.newPage();
document.add(new Paragraph("Text"));
document.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
class MyPageEvents extends PdfPageEventHelper {
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
System.out.println("onGenericTag");
}
public void onOpenDocument(PdfWriter writer, Document document) {
System.out.println("onOpenDocument");
}
public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
System.out.println("onChapter");
}
public void onEndPage(PdfWriter writer, Document document) {
System.out.println("onEndPage");
}
public void onCloseDocument(PdfWriter writer, Document document) {
System.out.println("onCloseDocument");
}
}
PdfPageEvent: on Paragraph
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class OnParagraphEventPDF extends PdfPageEventHelper {
public void onParagraph(PdfWriter writer, Document document, float position) {
System.out.println("onParagraph");
}
public static void main(String[] args) {
Document document = new Document(PageSize.A6);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OnParagraphEventPDF.pdf"));
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
writer.setPageEvent(new OnParagraphEventPDF());
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}
PdfPageEvent: page end
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class OnEndPageEventPDF extends PdfPageEventHelper {
public static void main(String[] args)
{
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("OnEndPageEventPDF.pdf"));
writer.setPageEvent(new OnEndPageEventPDF());
document.open();
document.add(new Paragraph("text"));
document.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
public void onEndPage(PdfWriter writer, Document document) {
System.out.println("on page end event call back");
}
}
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();
}
}