I am generating a PDF in my application using itext. In the footer part, a phrase containing page X of Y is there. The total number of pages is not showing after PDF creation. Please refer the below code:
in MainActivity:
PdfPTable table = new PdfPTable(2);
table.setSpacingAfter(40.0f);
table.setTotalWidth(document.right()-document.left()-100);
try {
table.setWidths(new int[]{1, 2});
} catch (DocumentException e) {
e.printStackTrace();
}
PdfPCell cellOne = new PdfPCell(img);
Font f=new Font(Font.FontFamily.TIMES_ROMAN,20.0f,Font.BOLD, BaseColor.BLACK);
Font Para1_font=new Font(Font.FontFamily.TIMES_ROMAN,15.0f,Font.BOLD, BaseColor.BLACK);
Paragraph paragraph1_header = new Paragraph("QUOTE TO: 294087",f);
Paragraph paragraph = new Paragraph();
paragraph.add("AAAAAAAAAALLC * hhjkhhhhuhjbbnb" +
"jgjkll;, Sjklkjjjhh * AAHHGBJJ");
// Paragraph paragraph3 = new Paragraph();
// paragraph3.add("Page 1");
paragraph.setAlignment(Paragraph.ALIGN_LEFT);
PdfPCell cellTwo = new PdfPCell(paragraph);
// PdfPCell cellThree = new PdfPCell(paragraph3);
cellTwo.setPaddingLeft(10.0f);
cellOne.setPaddingBottom(20.0f);
cellTwo.setPaddingBottom(20.0f);
cellThree.setPaddingBottom(20.0f);
cellOne.setBorder(Rectangle.NO_BORDER);
cellTwo.setBorder(Rectangle.NO_BORDER);
cellThree.setBorder(Rectangle.NO_BORDER);
cellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
cellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
cellThree.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cellOne);
table.addCell(cellTwo);
//table.addCell(cellThree);
try {
HeaderFooterPageEvent event = new HeaderFooterPageEvent(this, table);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
//writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
document.open();
inside HeaderFooterPageEvent class:
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(30, 16);
try {
totalPages = Image.getInstance(total);
} catch (BadElementException e) {
e.printStackTrace();
}
totalPages.setRole(PdfName.ARTIFACT);
}
public void onEndPage(PdfWriter writer, Document document) {
footer.writeSelectedRows(0, -100, 36, 65, writer.getDirectContent());
try {
PdfPCell cell = new PdfPCell(Image.getInstance(total));
} catch (BadElementException e) {
e.printStackTrace();
}
Phrase footerPhrase = new Phrase("Page "+writer.getPageNumber()+
" of");
footerPhrase.add(new Chunk(totalPages,0,0,true));
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footerPhrase, 500, 65, 0);
}
Its just showing " Page x of" instead of " Page X of Y". Is there something I am missing? Please help.