0

我正在尝试创建一个 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;
    }
}
4

1 回答 1

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为添加到每一页。如果您更改一页上的字段值,则它会在每一页上更改:

在此处输入图像描述

更新:

作为替代方案,您可以在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 文件中创建不同组件的一种非常简洁的方法。

于 2016-11-02T15:52:35.810 回答