Skip to Content
NexPDF

Forms

List, fill, create, recalculate and flatten PDF form fields, and exchange form data as JSON, FDF or XFDF, with NexPDF for Java.

English

License module

Every form call needs the forms module (Indie tier and above).

This page covers AcroForm fields. For XFA forms (XML Forms Architecture), see XFA forms. To fill a PDF from JSON data with placeholders, see Templates.

List the fields

formFieldsJson() returns a JSON array. The example reads it with Jackson; any JSON library works.

ObjectMapper json = new ObjectMapper();
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("application.pdf"))) {
    for (JsonNode f : json.readTree(doc.formFieldsJson())) {
        System.out.println(f.get("name").asText() + " " + f.get("kind").asText() + " " + f.get("value")
                + (f.get("required").asBoolean() ? " required" : ""));
    }
}

Each field has name, kind, value, page, the rectangle (llx, lly, urx, ury), read_only and required.

Fill fields

fillFormField(name, value) sets a value as a viewer would: the field's keystroke and validate scripts check it, then every calculated field is recomputed.

doc.fillFormField("applicant.name", "Ana Silva");
doc.fillFormField("quantity", "4");          // calculated totals update
doc.fillFormField("accept_terms", "true");   // check boxes take "true" or "false"
doc.fillFormField("country", "Portugal");    // list fields take the option text
doc.save(Path.of("filled.pdf"));

A value rejected by a validate script throws NexPdfException with the script's message.

Field appearances are drawn in embedded Unicode fonts (the bundled Liberation fonts, then registered fallback fonts), shaped with OpenType and ordered with Unicode bidi, so Arabic, Hebrew, Indic and other scripts show correctly. Right-to-left text without explicit alignment starts at the right.

Fill many fields from JSON

doc.importFormJson("{\"applicant.name\":\"Ana Silva\",\"quantity\":\"4\"}");
String values = doc.exportFormJson();

FDF and XFDF

byte[] fdf = doc.exportFormFdf("application.pdf");
String xfdf = doc.exportFormXfdf("application.pdf");

try (NexPdfDocument blank = NexPdfDocument.open(Path.of("application.pdf"))) {
    blank.importFormFdf(fdf);
    blank.save(Path.of("from-fdf.pdf"));
}

Add fields

addFormField(pageIndex, kind, name, llx, lly, urx, ury) creates a field. kind is text, checkbox, radio, combobox, listbox, button, signature or barcode.

doc.addFormField(0, "text", "customer", 100, 700, 300, 720);
doc.addFormField(0, "checkbox", "newsletter", 100, 670, 114, 684);
doc.addFormField(0, "signature", "signature", 100, 100, 300, 150);

Put a signature picture in a signature field

A drawn, typed or scanned signature can be shown in a signature field without signing the document digitally, as when a person signs a form on screen. setSignatureAppearance(name, rgba, width, height) takes the picture as RGBA pixels (width × height × 4 bytes; transparent pixels stay transparent), scales it to fit every widget of the field and centres it. The field must be a signature field that is not read-only. A later digital signature into the same field replaces the picture. The field list then reports has_appearance: true for the field.

BufferedImage img = ImageIO.read(new File("signature.png"));
int w = img.getWidth(), h = img.getHeight();
byte[] rgba = new byte[w * h * 4];
for (int y = 0, i = 0; y < h; y++)
    for (int x = 0; x < w; x++, i += 4) {
        int p = img.getRGB(x, y);
        rgba[i] = (byte) (p >> 16); rgba[i + 1] = (byte) (p >> 8); rgba[i + 2] = (byte) p; rgba[i + 3] = (byte) (p >>> 24);
    }
doc.setSignatureAppearance("signature", rgba, w, h);

The field list also describes how each field is drawn, so a custom UI can lay its own controls over the page: align (left, center or right), font_size (null when the size is automatic), text_color (#rrggbb); text fields add multiline, password, comb, scroll and max_len; signature fields add signed (signed digitally) and has_appearance.

Recalculate and flatten

doc.recalculateForms();   // runs every calculate script; returns the number of fields changed
doc.flattenForms();       // burns the values into the page; the form is no longer editable
doc.save(Path.of("final.pdf"));

On this page