[将该信息添加到文档页脚中时如何在iText7中获得总页数?

问题描述 投票:0回答:1

在iTextSharp中,我们可以覆盖OnCloseDocument()事件,并在文档的页脚中添加#total的Page#。但是,PdfDocument不再具有此文档关闭事件。由于添加新页面时无法确定页面总数,因此在生成文档时如何确定该总数并将其放在页脚中?

我已经看到了一些使用蛮力方法的建议:在生成PDF文档之后并且在刷新它之前,请使用PdfReader对其进行通读以获取总数,然后再更新页脚。这是唯一的方法吗?有没有更好的方法呢?

谢谢。

footer itext7
1个回答
0
投票
code sample中描述的另一种方法:

protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Document doc = new Document(pdfDoc); int numberOfPages = pdfDoc.getNumberOfPages(); for (int i = 1; i <= numberOfPages; i++) { // Write aligned text to the specified by parameters point doc.showTextAligned(new Paragraph(String.format("page %s of %s", i, numberOfPages)), 559, 806, i, TextAlignment.RIGHT, VerticalAlignment.TOP, 0); } doc.close(); }

在该代码示例中,您将在创建和刷新PDF之后添加页脚。这是执行此请求的非常简单的方法。

© www.soinside.com 2019 - 2024. All rights reserved.