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 Python.

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

import nexpdf

with nexpdf.Document.open("application.pdf") as doc:
    for f in nexpdf.loads(doc.form_fields_json()):
        print(f["name"], f["kind"], repr(f["value"]), "required" if f["required"] else "")

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

Fill fields

fill_form_field(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.fill_form_field("applicant.name", "Ana Silva")
doc.fill_form_field("quantity", "4")          # calculated totals update
doc.fill_form_field("accept_terms", "true")   # check boxes take "true" or "false"
doc.fill_form_field("country", "Portugal")    # list fields take the option text
doc.save("filled.pdf")

A value rejected by a validate script raises nexpdf.NexPdfError with the script's message.

Field appearances are drawn in embedded Unicode fonts (the bundled Liberation fonts, then any fallback font registered with nexpdf.register_fallback_font), shaped with OpenType and ordered with the Unicode bidi algorithm, so Arabic, Hebrew, Indic and CJK values display correctly. Right-to-left text without an explicit alignment starts at the right, and copied text comes back in logical order.

Fill many fields from JSON

import json

doc.import_form_json(json.dumps({"applicant.name": "Ana Silva", "quantity": "4"}))
values = nexpdf.loads(doc.export_form_json())

FDF and XFDF

fdf = doc.export_form_fdf("application.pdf")      # bytes
xfdf = doc.export_form_xfdf("application.pdf")    # str

with nexpdf.Document.open("application.pdf") as blank:
    blank.import_form_fdf(fdf)
    blank.save("from-fdf.pdf")

Add fields

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

doc.add_form_field(0, "text", "customer", 100, 700, 300, 720)
doc.add_form_field(0, "checkbox", "newsletter", 100, 670, 114, 684)
doc.add_form_field(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. set_signature_appearance(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.

from PIL import Image

img = Image.open("signature.png").convert("RGBA")
doc.set_signature_appearance("signature", img.tobytes(), img.width, img.height)
doc.save("signed-on-screen.pdf")

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.recalculate_forms()   # runs every calculate script; returns the number of fields changed
doc.flatten_forms()       # burns the values into the page; the form is no longer editable
doc.save("final.pdf")

On this page