0

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.

Animesh Jena
  • 1,541
  • 1
  • 25
  • 44

1 Answers1

1

You have implemented the onOpenDocument method to create a PdfTemplate of size 30, 16 (isn't that rather small?) and you are adding this empty placeholder on every page in the onEndPage method.

However, I don't see you adding content to the PdfTemplate anywhere. If you don't add the total number of pages, then you won't see the total number of pages anywhere in your document.

Since you can only know the total number of pages at the moment you close the document, you need to implement the onCloseDocument():

public void onCloseDocument(PdfWriter writer, Document document) {
    ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
            new Phrase(String.valueOf(writer.getPageNumber() - 1)),
            2, 2, 0);
}

See MovieCountries1 for a full example. This example was written in the context of the second edition of the book "iText in Action."

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165