如何在 iText 7 中添加 "Page X of Y" 页脚

How to add a "Page X of Y" footer in iText 7

我正在尝试弄清楚如何将 "Page X of Y" 页脚添加到我使用 iText 7 生成的 PDF 文档的每一页。

使用 IEventHandler 生成 "Page X" 部分似乎相当简单 - 这是我正在努力解决的 "of Y" 位。我想避免生成整个文档两次以找出它有多少页,因为这会严重影响性能!

我在 iText 5 中找到了解决方案:PDF Page Numbering in Java & iText,但是 iText 7 是一个完整的解决方案用完全不同的界面重写 iText,到目前为止我找不到任何类似的 iText 7 示例。

answer you found for iText 5 references the MovieCountries1 example. This example has been rewritten for iText 7 as Listing_05_20_MovieCountries1。其关键代码:

protected PdfFont bold;
protected PdfFont italic;
protected PdfFont normal;

protected PdfFormXObject template;

public void manipulatePdf(String dest) throws IOException, SQLException {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdfDoc, new PageSize(PageSize.A4), true);
    doc.setMargins(54, 36, 36, 36);

    bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
    italic = PdfFontFactory.createFont(FontConstants.HELVETICA_OBLIQUE);
    normal = PdfFontFactory.createFont(FontConstants.HELVETICA);

    template = new PdfFormXObject(new Rectangle(550, 803, 30, 30));
    PdfCanvas canvas = new PdfCanvas(template, pdfDoc);

    HeaderHandler headerHandler = new HeaderHandler();
    pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);

    ... ADD CONTENT ...

    canvas.beginText();
    try {
        canvas.setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 12);
    } catch (IOException e) {
        e.printStackTrace();
    }
    canvas.moveText(550, 803);
    canvas.showText(Integer.toString(pdfDoc.getNumberOfPages()));
    canvas.endText();
    canvas.release();

    doc.close();
}

public class HeaderHandler implements IEventHandler {
    protected String country;

    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfPage page = docEvent.getPage();
        int pageNum = docEvent.getDocument().getPageNumber(page);
        PdfCanvas canvas = new PdfCanvas(page);
        canvas.beginText();
        try {
            canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 12);
        } catch (IOException e) {
            e.printStackTrace();
        }
        canvas.moveText(34, 803);
        canvas.showText(country);
        canvas.moveText(450, 0);
        canvas.showText(String.format("Page %d of", pageNum));
        canvas.endText();
        canvas.stroke();
        canvas.addXObject(template, 0, 0);
        canvas.release();
    }

    public void setHeader(String country) {
        this.country = country;
    }
}

您还会在那里找到许多其他示例的重写。


正如@Bruno 在评论中所说,甚至还有一个真正为 iText 7 创建的略有不同的示例(与上面的示例相反,它是 iText 5 示例的端口)。

这是 chapter 7 of the iText 7: building blocks tutorial 附带的示例。它使用 showTextAligned() 来确保“第 X 页”与“Y”完美匹配,无论 X 和 Y 有多少个数字,请参见。它的页尾事件侦听器方法:

public void handleEvent(Event event) {
    PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
    PdfDocument pdf = docEvent.getDocument();
    PdfPage page = docEvent.getPage();
    int pageNumber = pdf.getPageNumber(page);
    Rectangle pageSize = page.getPageSize();
    PdfCanvas pdfCanvas = new PdfCanvas(
        page.newContentStreamBefore(), page.getResources(), pdf);
    Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
    Paragraph p = new Paragraph()
        .add("Page ").add(String.valueOf(pageNumber)).add(" of");
    canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
    pdfCanvas.addXObject(placeholder, x + space, y - descent);
    pdfCanvas.release();
}

(C07E03_PageXofY.java, 事件处理程序 class PageXofY)