[英]Using iText7 (Java) to add a table to an existing PDF and continue on additional pages
I am attempting to complete a project with almost identical requirements as those associated with this question asked in 2015. 我正在尝试完成一个几乎与2015年提出的与此问题相关的要求完全相同的项目。
The answer provided by Bruno was perfect, but related to iText5. Bruno提供的答案是完美的,但与iText5有关。 I am relatively new to iText, and am desperately trying to get up-to-speed to complete a current project.
我对iText来说还比较陌生,并且极力地尝试着加快完成当前项目的速度。
Can anyone assist with the translation of Bruno's answer from iText5 to iText7? 有人可以协助将Bruno的答案从iText5转换为iText7吗?
Thanks so much in advance for any/all assistance! 提前非常感谢您提供的任何/所有帮助!
You should write something like that: 您应该这样写:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
Document doc = new Document(pdfDoc);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("Name").setValue("Jeniffer");
fields.get("Company").setValue("iText's next customer");
fields.get("Country").setValue("No Man's Land");
form.flattenFields();
Table table = new Table(UnitValue.createPercentArray(new float[]{1, 15}));
table.addHeaderCell("#");
table.addHeaderCell("description");
for (int i = 1; i <= 150; i++) {
table.addCell(String.valueOf(i));
table.addCell("test " + i);
}
doc.setRenderer(new DocumentRenderer(doc) {
@Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
LayoutArea area = super.updateCurrentArea(overflowResult);
if (area.getPageNumber() == 1) {
area.getBBox().decreaseHeight(266);
}
return area;
}
});
doc.add(table);
doc.close();
Probably the most interesting part is about extending DocumentRenderer. 可能最有趣的部分是关于扩展DocumentRenderer。 The instance of this class associated with document handles its layout and overrided method (updateCurrentArea), as the name stands for, updates area for layout.
此类的实例(与文档关联)处理其布局和覆盖的方法(updateCurrentArea),顾名思义,该实例用于布局的更新区域。
What is important to mention: All iText5 SO answers are ported in iText7 and you can find them on iText's website : https://developers.itextpdf.com/content/itext-7-examples . 值得一提的是: 所有iText5 SO答案都已移植到iText7中,您可以在iText网站上找到它们 : https : //developers.itextpdf.com/content/itext-7-examples 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.