Itext 7,不能使用两个引用相同表单值的小部件

时间:2016-11-02 15:15:28

标签: java pdf pdf-generation itext7

我正在尝试使用pdf,其中多个小部件引用相同的表单值,以便在填充一个小部件时,所有其他小部件显示相同的值。我可以在引用不同的表单字段时添加多个小部件,但是当我更改它们以引用相同的表单字段时,只显示一个小部件。这是我的示例代码:

public class HelloWorld {

    public static final String DEST = "sampleOutput.pdf";
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);

        new HelloWorld().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException {
        //Initialize PDF writer
        PdfWriter writer = new PdfWriter(dest);

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(writer);

        // Initialize document
        Document document = new Document(pdf);

        HelloWorld.addAcroForm(pdf, document);

        //Close document
        document.close();
    }

    public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
        Paragraph title = new Paragraph("Test Modify")
                .setTextAlignment(TextAlignment.CENTER)
                .setFontSize(16);
        doc.add(title);
        doc.add(new Paragraph("Confirmation:").setFontSize(12));

        //Add acroform
        PdfAcroForm form = PdfAcroForm.getAcroForm(doc.getPdfDocument(), true);

        //Create text field
        PdfTextFormField nameField = PdfFormField.createText(doc.getPdfDocument(),
                new Rectangle(99, 525, 425, 15), "name", "");

        PdfTextFormField nameField2 = PdfFormField.createText(doc.getPdfDocument(),
                new Rectangle(99, 425, 425, 15), "name2", "");//"name2" works, if I use "name" only nameField is shown (not nameField2)

        form.addField(nameField, pdf.getFirstPage());
        form.addField(nameField2, pdf.getFirstPage());

        return form;
    }
}

1 个答案:

答案 0 :(得分:2)

我理解您的问题的方式,您想要添加一个字段,例如name,PDF文件,并且您希望将此字段的窗口小部件注释添加到每个页面。

我已经调整了你的代码:

public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
    Paragraph title = new Paragraph("Test Form")
            .setTextAlignment(TextAlignment.CENTER)
            .setFontSize(16);
    doc.add(title);
    doc.add(new Paragraph("Full name:").setFontSize(12));

    PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
    PdfTextFormField nameField = PdfFormField.createText(pdf,
            new Rectangle(99, 525, 425, 15), "name", "");
    for (int i = 1; i < 8; i++) {
        form.addField(nameField, pdf.getPage(i));
    }
    return form;
}

现在您有一个名为name的字段,该字段已添加到每个页面。如果您在一个页面上更改字段的值,则会在每个页面上更改该字段:

enter image description here

<强>更新

作为替代方案,您可以先创建PdfTextFormField,而无需先定义Rectangle。您可以向页面添加不同的窗口小部件注释,然后将这些窗口小部件注释作为孩子添加到该字段中:

public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
    PdfTextFormField nameField = PdfFormField.createText(pdf);
    nameField.setFieldName("name");
    PdfWidgetAnnotation widget1 = new PdfWidgetAnnotation(new Rectangle(99, 525, 425, 15));
    pdf.getPage(1).addAnnotation(widget1);
    PdfWidgetAnnotation widget2 = new PdfWidgetAnnotation(new Rectangle(99, 425, 425, 15));
    pdf.getPage(1).addAnnotation(widget2);
    form.addField(nameField, pdf.getPage(1));
    nameField.addKid(widget1);
    nameField.addKid(widget2);
    return form;
}

这是在PDF文件中创建不同组件的一种非常简洁的方法。