gpt4 book ai didi

java - 如何获取特定 pdf 表单字段的页码和位置,并在 iText7 中的相应位置插入表格?

转载 作者:行者123 更新时间:2023-12-01 16:47:38 36 4
gpt4 key购买 nike

我想在表单字段的位置插入一个表格。如何获取表单域的位置和页码?如何将表格插入到该位置?

//Retrieves AcroForm from the document.
//If there is no AcroForm in the document Catalog and createIfNotExist
//flag is true then the AcroForm dictionary will be created and added to the document.
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDoc, true);
//Gets the form fields as a Map.
Map<String, PdfFormField> fields = acroForm.getFormFields();
//Create Table
Table table = new Table(new float[]{1,1});
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.setWidthPercent(95);
Cell cell = new Cell();
cell.add(new Paragraph("Name"));
//cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
Cell cell = new Cell();
cell.add(new Paragraph("Address"));
//cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
...
...
doc.add(table);
...

最佳答案

字段本身没有任何位置或页码。但是,它的小部件注释可以。

要访问这些,您可以使用field.getWidgets()。然后,您可以使用 annotation.getPage()annotation.getRectangle() 获取有关注释位置的信息。

要在某个特定位置布局单个元素,最好的选择之一是使用 Canvas 布局对象。然后可以使用 page.removeAnnotation(annotation); 删除注释。

总的来说,这会编译成以下解决方案:

String fieldName = "Text1";
PdfFormField field = form.getField(fieldName);

for (PdfAnnotation annotation : field.getWidgets()) {
PdfPage page = annotation.getPage();
Rectangle rectangle = annotation.getRectangle().toRectangle();

Table table = new Table(UnitValue.createPointArray(new float[] {-1, -1}));
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
Cell cell = new Cell();
cell.add(new Paragraph("Name"));
table.addCell(cell);
cell = new Cell();
cell.add(new Paragraph("Address"));
table.addCell(cell);

Canvas canvas = new Canvas(new PdfCanvas(page), pdfDocument, rectangle);
canvas.add(table);
canvas.close();

page.removeAnnotation(annotation);
}

当然,您必须完全负责创建适当大小的表格,以使其适合您想要的区域。您可以使用较小的字体大小等。

关于java - 如何获取特定 pdf 表单字段的页码和位置,并在 iText7 中的相应位置插入表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023654/

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