Skip to Content
NexPDF

Forms

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

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

for (const f of doc.formFields()) {
  console.log(f.name, f.kind, f.value, f.required ? 'required' : '');
}

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

Fill fields

fillField(name, value) sets a value as a viewer would: the field's keystroke and validate scripts check it, then every calculated field is recomputed. Numbers and booleans are converted to text.

doc.fillField('applicant.name', 'Ana Silva');
doc.fillField('quantity', 4);          // calculated totals update
doc.fillField('accept_terms', true);   // check boxes take "true" or "false"
doc.fillField('country', 'Portugal');  // list fields take the option text
const blob = doc.toBlob();

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

Field appearances are drawn in embedded Unicode fonts, with OpenType shaping and the Unicode bidi algorithm, so Arabic, Hebrew, Indic and other scripts show correctly. Right-to-left text without an explicit alignment starts at the right, and copied or extracted text comes back in logical order.

Fill many fields from JSON

doc.raw.importFormJson(JSON.stringify({ 'applicant.name': 'Ana Silva', quantity: '4' }));
const values = JSON.parse(doc.raw.exportFormJson());

FDF and XFDF

const fdf = doc.raw.exportFormFdf('application.pdf');     // Uint8Array
const xfdf = doc.raw.exportFormXfdf('application.pdf');   // string

const blank = await nex.PdfDocument.fromUrl('/application.pdf');
blank.raw.importFormFdf(fdf);

Add fields

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

doc.raw.addFormField(0, 'text', 'customer', 100, 700, 300, 720);
doc.raw.addFormField(0, 'checkbox', 'newsletter', 100, 670, 114, 684);
doc.raw.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. doc.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.

// `canvas` holds the signature the user drew.
const ctx = canvas.getContext('2d')!;
const { data, width, height } = ctx.getImageData(0, 0, canvas.width, canvas.height);
doc.setSignatureAppearance('signature', new Uint8Array(data.buffer), width, height);

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

On this page