Forms
List, fill, create, recalculate and flatten PDF form fields on the device, and exchange form data as JSON, FDF or XFDF, with NexPDF for React Native.
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
const doc = await NexPdf.open(base64Pdf);
for (const f of await 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
The native readers (iOS, Android) let the user fill fields on the page and add a drawn, imported or typed signature to signature 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.
await doc.fillFormField('applicant.name', 'Ana Silva');
await doc.fillFormField('quantity', '4'); // calculated totals update
await doc.fillFormField('accept_terms', 'true'); // check boxes take "true" or "false"
await doc.fillFormField('country', 'Portugal'); // list fields take the option text
const filled = await doc.saveBytes();A value rejected by a validate script rejects with NexPdfError and the script's message.
The new appearance of a text field is drawn in an embedded Unicode font (the bundled Liberation
fonts, then any fallback font registered with NexPdf.api.registerFallbackFont), shaped with OpenType and
ordered with the Unicode bidi algorithm, so Arabic, Hebrew, Indic and CJK values show as typed.
Right-to-left text without an explicit alignment starts at the right, and copying or extracting
the text returns it in logical order.
Fill many fields from JSON
await doc.importFormJson(JSON.stringify({ 'applicant.name': 'Ana Silva', quantity: '4' }));
const values = JSON.parse(await doc.exportFormJson());FDF and XFDF
const fdf = await doc.exportFormFdf('application.pdf'); // base64
const xfdf = await doc.exportFormXfdf('application.pdf'); // string
const blank = await NexPdf.open(blankBase64);
await blank.importFormFdf(fdf);Add fields
addFormField(page, kind, name, llx, lly, urx, ury) creates a field. kind is text, checkbox,
radio, combobox, listbox, button, signature or barcode.
await doc.addFormField(0, 'text', 'customer', 100, 700, 300, 720);
await doc.addFormField(0, 'checkbox', 'newsletter', 100, 670, 114, 684);
await 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, rgbaBase64, 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.
// rgbaBase64: the RGBA pixels of the signature, base64-encoded.
await doc.setSignatureAppearance('signature', rgbaBase64, 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
await doc.recalculateForms(); // runs every calculate script; resolves to the number of fields changed
await doc.flattenForms(); // burns the values into the page; the form is no longer editable