we use itextpdf and xmlworker APIs ( itextpdf-5.1.1.jar, Xmlworker-1.1.0.jar) for PDF generation tasks in our application. We generate HTML content from XML-XSLT conversion and then use the HTML content to create pdf document.
When we trying to implement pagination ( as 1 of 3, 2 of 3..), we learnt from itextpdf online samples that we always need to create a resultant pdf document apart from the PDF document that we create in order to stamp the page number on the each page content.
This approach brings challenges in removing the intermediate pdf document. Is there any way by which page numbers can be determined during the time we create the pdf very first time so that we will avoid creating one resultant pdf document?
Thanks Venkat
class TableFooter extends PdfPageEventHelper {
String header;
PdfTemplate total;
public void setHeader(String header) {
this.header = header;
}
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(30, 16);
}
public void onEndPage(PdfWriter writer, Document document) {
Font ffont = new Font(Font.FontFamily.UNDEFINED, 7, Font.NORMAL);
PdfPTable table = new PdfPTable(2);
try
{
table.setWidths(new int[]{1,1});
table.setTotalWidth(50);
table.getDefaultCell().setFixedHeight(15);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
Phrase footer = new Phrase(String.format("%d of", writer.getPageNumber()), ffont);
table.addCell(footer);
PdfPCell cell = new PdfPCell(Image.getInstance(total));
table.addCell(cell);
table.writeSelectedRows(0, -1, 275, 20, writer.getDirectContent());
}
catch(Exception de) {
throw new ExceptionConverter(de);
}
}
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber() - 1)), 10, 4, 0);
}
}