[英]Using iText7 (Java) to add a table to an existing PDF and continue on additional pages
我正在嘗試完成一個幾乎與2015年提出的與此問題相關的要求完全相同的項目。
Bruno提供的答案是完美的,但與iText5有關。 我對iText來說還比較陌生,並且極力地嘗試着加快完成當前項目的速度。
有人可以協助將Bruno的答案從iText5轉換為iText7嗎?
提前非常感謝您提供的任何/所有幫助!
您應該這樣寫:
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();
可能最有趣的部分是關於擴展DocumentRenderer。 此類的實例(與文檔關聯)處理其布局和覆蓋的方法(updateCurrentArea),顧名思義,該實例用於布局的更新區域。
值得一提的是: 所有iText5 SO答案都已移植到iText7中,您可以在iText網站上找到它們 : https : //developers.itextpdf.com/content/itext-7-examples 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.