gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:56:17 24 4
gpt4 key购买 nike

我试图弄清楚如何向PDF文档中的每个页面添加“Y的X页”页脚,这是我使用 iText 7 生成的。

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

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

最佳答案

answer you found for iText 5引用了MovieCountries1示例。此示例已为iText 7重写为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(FontConstants.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 7iText 7: building blocks tutorial附带的示例。它使用 showTextAligned()来确保“X的第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,事件处理程序类 PageXofY)

关于itext - 如何在iText 7中添加 "Page X of Y"页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38588704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com