itext 页脚中未显示的总页数

Total number of pages in itext footer not showing

我正在使用 itext 在我的应用程序中生成 PDF。在页脚部分,有一个包含页面 X of Y 的短语。创建 PDF 后未显示总页数。请参考以下代码:

在 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();

在 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);


    }

它只显示“第 x 页”而不是“第 X 页,共 Y”。有什么我想念的吗?请帮忙。

您已经实现了 onOpenDocument 方法来创建大小为 30、16(是不是相当小?)的 PdfTemplate 并且您正在添加这个 empty[= onEndPage 方法中每个页面上的 26=] 占位符。

但是,我没有看到您在任何地方向 PdfTemplate 添加内容。如果您不添加总页数,那么您将不会在文档的任何位置看到总页数。

由于只能在关闭文档的那一刻知道总页数,所以需要实现onCloseDocument():

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

有关完整示例,请参阅 MovieCountries1。这个例子是在本书第二版的上下文中写的 "iText in Action."