如何添加"Y的第X页" iText 7中的页脚 [英] How to add a "Page X of Y" footer in iText 7
问题描述
我试图弄清楚如何在使用 iText 7 生成的PDF文档的每个页面中添加"Y的X页"页脚.
I'm trying to figure out how to add a "Page X of Y" footer to each page in a PDF document, which I'm generating using iText 7.
使用IEventHandler生成"Page X"部分似乎很简单-这是我一直在努力的"of of Y"位.我想避免生成整个文档两次,以查明它有多少页,因为这会严重影响性能!
Using an IEventHandler to generate the "Page X" part seems fairly straightforward - it's the "of Y" bit that I'm struggling with. I want to avoid generating the whole document twice in order to find out how many pages it has, as this would be a significant performance hit!
I've found a solution for this in iText 5 here: PDF Page Numbering in Java & iText, but iText 7 is a complete rewrite of iText with a totally different interface and so far I've been unable to find any similar iText 7 examples.
推荐答案
回答您为iText 5 找到的内容引用了 MovieCountries1 示例.对于iText 7,此示例已重写为
The answer you found for iText 5 references the MovieCountries1 example. This example has been rewritten for iText 7 as Listing_05_20_MovieCountries1. Its pivotal code:
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;
}
}
您还将在这里找到许多其他示例的重写.
You'll find rewrites of many other samples there, too.
正如@Bruno在评论中指出的,甚至还有一个稍稍不同的示例,该示例是为iText 7真正创建的(与上面的示例是iText 5示例的端口相反).
As @Bruno remarked in a comment, there even is a slightly different example which has been genuinely created for iText 7 (in contrast to the example above which is the port of an iText 5 example).
这是第7章"rel =" noreferrer> iText 7 :积木教程.它使用showTextAligned()
确保"X的第X页"与"Y"很好地匹配,而不管X和Y有多少个数字,请参见.其页面结束事件监听器方法:
It's an example accompanying the chapter 7 of the iText 7: building blocks tutorial. It uses showTextAligned()
to make sure the "Page X of" nicely matches with the "Y", no matter how many digits X and Y have, cf. its end-of-page event listener method:
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
)
(C07E03_PageXofY.java, event handler class PageXofY
)
这篇关于如何添加"Y的第X页" iText 7中的页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!