PdfTextFormField - 2 synchron Fields - How to fill in an initial value? - iText 7.1.0 for Java

At the moment I'm writing a couple of evaluatuation programs with iText.

I have an issue with 2 fields which should always have the same value (2 represantations of 1 field).

In the final version these fields are on different pages at an arbitrary position.

Setting the value with field.SetValue gives an error. Setting the value with widget1.setContents does nothing.

Has someone an idea how to solve this problem?

Thanks, Dirk

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation;

public class problem2 {
    public static void main(String[] args) throws IOException {
        String fnPdf = "results/problem2.pdf";
        PdfWriter writer = new PdfWriter(fnPdf);
        PdfDocument pdf = new PdfDocument(writer);
        PdfPage page = pdf.addNewPage();
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
        PdfTextFormField field = PdfFormField.createText(pdf);
        field.setFieldName("fName");
        // field.setValue("test"); // produces an error
        Rectangle rect1 = new Rectangle(40, 200, 150, 20);
        PdfWidgetAnnotation widget1 = new PdfWidgetAnnotation(rect1);
        widget1.setContents("test"); // no error but does'nt work
        page.addAnnotation(widget1);
        Rectangle rect2 = new Rectangle(240, 200, 150, 20);
        PdfWidgetAnnotation widget2 = new PdfWidgetAnnotation(rect2);
        widget2.setContents("test"); // no error but does'nt work
        page.addAnnotation(widget2);
        form.addField(field, page);
        field.addKid(widget1);
        field.addKid(widget2);
        pdf.close();
        Desktop.getDesktop().open(new File(fnPdf));
    }
}

1 answer

  • answered 2018-01-14 12:15 Alexey Subach

    You need to change the order of the operations you perform a little bit. @mkl is correct in that you first have to set up the structure and then change the value of a field.

    If you do this you don't need to use setContents and calling setValue is enough.

    Also, make sure that the widgets are indirect objects: widget.makeIndirect(pdf);

    The full code snippet that produces the desired field with two widgets that share field's value:

    PdfWriter writer = new PdfWriter(fnPdf);
    PdfDocument pdf = new PdfDocument(writer);
    PdfPage page = pdf.addNewPage();
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
    PdfTextFormField field = PdfFormField.createText(pdf);
    field.setFieldName("fName");
    
    Rectangle rect1 = new Rectangle(40, 200, 150, 20);
    PdfWidgetAnnotation widget1 = new PdfWidgetAnnotation(rect1);
    widget1.makeIndirect(pdf);
    page.addAnnotation(widget1);
    field.addKid(widget1);
    
    Rectangle rect2 = new Rectangle(240, 200, 150, 20);
    PdfWidgetAnnotation widget2 = new PdfWidgetAnnotation(rect2);
    widget2.makeIndirect(pdf);
    page.addAnnotation(widget2);
    field.addKid(widget2);
    
    field.setValue("test");
    
    form.addField(field, page);
    pdf.close();