Java 如何获取特定pdf表单字段的页码和位置并在其中插入表格';iText7有什么地方?

Java 如何获取特定pdf表单字段的页码和位置并在其中插入表格';iText7有什么地方?,java,pdf,itext7,Java,Pdf,Itext7,我想在表单字段的位置插入一个表。如何获取表单字段的位置和页码?如何在该位置插入表 //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

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

//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);
...
//从文档中检索AcroForm。
//如果文档目录和createIfNotExist中没有AcroForm
//如果标志为true,则将创建AcroForm字典并将其添加到文档中。
PdfAcroForm acroForm=PdfAcroForm.getAcroForm(pdfDoc,true);
//获取作为映射的表单字段。
Map fields=acroForm.getFormFields();
//创建表
Table Table=新表(新浮点[]{1,1});
表.setHorizontalAlignment(HorizontalAlignment.CENTER);
表1.设定宽度百分比(95);
单元格=新单元格();
添加(新段落(“名称”);
//单元格顺序(边框,无边框);
表2.addCell(cell);
单元格=新单元格();
单元格。添加(新段落(“地址”);
//单元格顺序(边框,无边框);
表2.addCell(cell);
...
...
单据新增(表);
...

字段本身没有任何位置或页码。然而,它的小部件注释确实如此

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

要在某个特定位置布局单个元素,最佳选择之一是使用
Canvas
layout对象。然后可以使用
页面删除注释。removeAnnotation(注释)

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

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);
}

当然,您必须全权负责创建一个适当大小的表,以便它适合您想要的区域。您可以使用较小的字体大小等。

字段本身没有任何位置或页码。然而,它的小部件注释确实如此

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

要在某个特定位置布局单个元素,最佳选择之一是使用
Canvas
layout对象。然后可以使用
页面删除注释。removeAnnotation(注释)

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

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);
}

当然,您必须全权负责创建一个适当大小的表,以便它适合您想要的区域。您可以使用较小的字体大小等。

谢谢@谢谢你@阿列克谢苏巴赫