【问题标题】:Total number of pages in itext footer not showingitext 页脚中的总页数未显示
【发布时间】:2017-12-10 05:22:01
【问题描述】:

我正在使用 itext 在我的应用程序中生成 PDF。在页脚部分,有一个包含 Y 页 X 的短语。创建 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 类中:

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”。有什么我想念的吗?请帮忙。

【问题讨论】:

    标签: android itext pdf-generation


    【解决方案1】:

    您已经实现了 onOpenDocument 方法来创建大小为 30、16 的 PdfTemplate(这不是很小吗?),并且您将在每个页面上添加这个 empty 占位符onEndPage 方法。

    但是,我没有看到您在任何地方向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》第二版的背景下编写的。

    【讨论】:

      猜你喜欢
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 2013-11-03
      相关资源
      最近更新 更多