Forms
Let users fill and create PDF form fields in the native reader, or list, fill and flatten fields from Dart through the NexPDF C API, with NexPDF for Flutter.
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.
In the reader
The native reader has a forms mode where the user fills fields and adds new
ones. Limit the field tools with formsConfig.availableTools:
await NexPdfReader.open(
document: pdfBytes,
config: NexPdfUIConfig.fromJson({
'modeConfig': {'initialMode': 'forms'},
'formsConfig': {'availableTools': ['text', 'checkbox', 'signature']},
}),
);In NexPdfViewerWidget, the user types straight into text fields on the page, ticks check boxes and radio buttons and picks from lists; each change runs the field's scripts and the page is drawn again. Tapping a signature field opens Add signature, with Draw (black, blue or indigo ink), Image and Type tabs, and puts the picture in the field. Read-only and digitally signed fields are not editable. Turn it off with formEditing: false; pass
signatureImagePicker (for example with image_picker) to show the Image tab. Changes emit
formFieldChanged and signatureAdded.
From code
The form functions have no Dart method. Call them
through dart:ffi on doc.handle:
import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';
// char *nexpdf_form_fields_json(const NexPdfDocument *doc);
final formFieldsJson = nexpdfLib.lookupFunction<
Pointer<Utf8> Function(Pointer<Void>), Pointer<Utf8> Function(Pointer<Void>)>('nexpdf_form_fields_json');
// int32_t nexpdf_fill_form_field(NexPdfDocument *doc, const char *name, const char *value);
final fillFormField = nexpdfLib.lookupFunction<
Int32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>),
int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_fill_form_field');
final fields = jsonDecode(nexpdfCall(() => takeString(formFieldsJson(doc.handle))) ?? '[]') as List;
for (final f in fields.cast<Map<String, Object?>>()) {
print('${f['name']} ${f['kind']} ${f['value']}');
}
void fill(String name, String value) => using((a) => nexpdfCall(() =>
fillFormField(doc.handle, name.toNativeUtf8(allocator: a), value.toNativeUtf8(allocator: a))));
fill('applicant.name', 'Ana Silva');
fill('quantity', '4'); // calculated totals update
fill('accept_terms', 'true'); // check boxes take "true" or "false"
fill('country', 'Portugal'); // list fields take the option text
final filled = doc.save();Each field has name, kind, value, page, the rectangle (llx, lly, urx, ury),
read_only and required. nexpdf_fill_form_field runs the field's keystroke and validate
scripts, then recomputes every calculated field; a rejected value throws NexPdfException with
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 sdk.invoke('register_fallback_font', [fontBytes])), 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.
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.
// ui.Image from the signature pad
final data = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
doc.setSignatureAppearance('signature', data!.buffer.asUint8List(), image.width, image.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.
Other form functions
| C function | Does |
|---|---|
nexpdf_import_form_json(doc, json), nexpdf_export_form_json(doc) | Fill many fields from {"name": "value"}, or read them all |
nexpdf_add_form_field(doc, page, kind, name, llx, lly, urx, ury) | Creates a field: text, checkbox, radio, combobox, listbox, button, signature or barcode |
nexpdf_recalculate_forms(doc) | Runs every calculate script; returns the number of fields changed |
nexpdf_flatten_forms(doc) | Burns the values into the page |
nexpdf_export_form_fdf, nexpdf_import_form_fdf, nexpdf_export_form_xfdf, nexpdf_import_form_xfdf | FDF and XFDF data exchange |