Search code examples
javapdfpdf-generationitext7

Itext 7, can't use two widgets referencing same form value


I am trying to have a pdf in which multiple widgets reference the same form value so that when one widget is filled, all the others show the same value. I am able to add multiple widgets when they reference different form fields, but when I change them to reference the same form field, only one widget shows. Here's my sample code:

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

Solution

  • The way I understand your question, you want to add one field, e.g. name, to a PDF file, and you want a widget annotation of this field to be added to each page.

    I've adapted your code like this:

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

    Now you have one field, named name that is added to every page. If you change the value of the field on one page, it is changed on every page:

    enter image description here

    Update:

    As an alternative, you can create your PdfTextFormField without defining a Rectangle first. You can add different widget annotations to a page, and then add these widget annotations as kids to the field:

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

    This is a very clean way of creating the different components in your PDF file.