Turn from: http://blog.chinaunix.net/uid-122937-id-3052063.html
1. Overview IText5 does not have a previous version of the HeaderFooter object to set headers and footers, you can use Pdfpageeventhelper to complete the header and footer setup work. The following event handlers are included in the Pdfpageeventhelper.
OnOpenDocument ()-Fires when a document is opened and can be used to initialize the global variables of the document.
OnStartPage ()-When a page is initialized, it can be used to initialize the setting parameters of the page, but note that when the function is triggered, the page is not created, and it is not necessary to use this function to add content, preferably using onendpage () to process the initialization of the page.
OnEndPage ()-The best time to add headers, footers, watermarks, and so on when creating a new page to complete but before writing the content.
OnCloseDocument ()-Triggers before the document closes, and can be used to free some resources.
2. Heavy Duty Pdfpageeventhelper Class
Defines a class HeaderFooter that inherits the parent class Pdfpageeventhelper
In each page of the document, you must define a ractangle rectangle with the argument art so that you can set the header and footer by getting the border position of the document through the rectangle in the HeaderFooter class.
The code is as follows:
Import com.itextpdf.text.Document;
Import com.itextpdf.text.Element;
Import Com.itextpdf.text.Phrase;
Import Com.itextpdf.text.Rectangle;
Import Com.itextpdf.text.pdf.ColumnText;
Import Com.itextpdf.text.pdf.PdfPageEventHelper;
Import Com.itextpdf.text.pdf.PdfWriter;
public class HeaderFooter extends pdfpageeventhelper{
public void OnEndPage (PDFWriter writer, document document) {Rectangle rect = writer.getboxsize ("Art");
Switch (Writer.getpagenumber ()% 2) {
Case 0:
Columntext.showtextaligned (Writer.getdirectcontent (),
Element.align_right, New Phrase ("Even header"),
Rect.getright (), Rect.gettop (), 0);
Break
Case 1:
Columntext.showtextaligned (Writer.getdirectcontent (),
Element.align_left, New Phrase ("Odd header"),
Rect.getleft (), Rect.gettop (), 0);
Break
}
Columntext.showtextaligned (Writer.getdirectcontent (),
Element.align_center, New Phrase (String.Format ("page%d", Writer.getpagenumber ()),
(Rect.getleft () + rect.getright ())/2, Rect.getbottom ()-18, 0);
}
The calling code is as follows
Import Com.itext.HeaderFooter;
Import Com.itextpdf.text.pdf.PdfPageEventHelper;
Import Com.itextpdf.text.pdf.PdfWriter;
Import Com.itextpdf.text.pdf.ColumnText;
Import com.itextpdf.text.*;
Import Java.io.FileOutputStream;
public class Headerandfooterdemo {
public static void Main (string[] args) {
Document document = new Document (PAGESIZE.A4, 50, 50, 50, 50);
try{
PDFWriter writer=pdfwriter.getinstance (document,
New FileOutputStream ("C:\\testheaderandfooter.pdf"));
Rectangle rect = new Rectangle (36, 54, 559, 788);
Rect.setbordercolor (Basecolor.black);
Writer.setboxsize ("Art", rect);
HeaderFooter header=new HeaderFooter ();
Writer.setpageevent (header);
Document.open ();
Document.newpage ();
Paragraph par = new Paragraph ("A-Paragraph");
Document.add (PAR);
Document.newpage ();
Paragraph par2 = new Paragraph ("Second Paragraph");
Document.add (PAR2);
Document.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
}
3. To resolve the issue of page X/CO y page
We get the current page by the Getpagenumber () function in PDFWriter, but there's no way to get a total number of pages for the document.
We can take advantage of the Xobject object, Itext writes pdftemplate to OutputStream only after invoking the release template method, otherwise the object is kept in memory until the document is closed.
We can add template to page 1th until the last page writes the content to this template.
Import com.itextpdf.text.Document;
Import com.<