XFA forms
Read XFA forms, export and import their data as XML or JSON, convert them to AcroForm, flatten them or remove XFA with NexPDF for Flutter.
License module
XFA forms need the forms module (Indie tier and above).
XFA (XML Forms Architecture) forms keep their layout and data in XML inside the PDF. Many government and bank forms use them, and many viewers no longer open them. NexPDF reads both kinds:
- static XFA, where the PDF also has AcroForm fields: the XFA data and the AcroForm fields are kept in step, so filling either side updates the other;
- dynamic XFA, where the pages are laid out from the XFA template: NexPDF lays the template out into ordinary PDF pages with AcroForm fields.
Inspect a form
import 'dart:io';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
final doc = sdk.open(await File(taxFormPath).readAsBytes());
final info = doc.xfaInfo();
print('${info['has_xfa']} ${info['dynamic']} ${info['field_count']} ${info['acroform_field_count']}');The result has has_xfa, dynamic, field_count, acroform_field_count, data_value_count
and the list of XDP packets.
Export and import data
doc.xfaExportData(format: ...) returns the data as xml (the data document, <form1>…</form1>,
the default) or as flat json text ({"path": "value"}). It throws NexPdfException when the
document has no XFA.
import 'dart:convert';
final values = jsonDecode(doc.xfaExportData(format: 'json')) as Map<String, Object?>;
print(values['form1.customer.name']);
doc.xfaImportData('{"form1.customer.name":"Ana Silva","form1.item[1].qty":"3"}', format: 'json');
await File(filledPath).writeAsBytes(doc.save());doc.xfaImportData(data, format: ...) returns the number of values imported; format is xml by
default. XML replaces the data; JSON (flat paths or nested objects) sets the given values and
creates repeated groups as needed. Data paths are dotted names with an index for repeated siblings
(form1.item[1].qty).
Convert, flatten or remove
| Call | Result |
|---|---|
xfaToAcroform() | A plain AcroForm PDF without XFA. Dynamic forms are laid out into new pages. Returns the number of fields |
xfaFlatten() | Converts and flattens: a plain PDF with the values drawn. Returns the number of flattened widgets |
xfaRemove() | Removes XFA and keeps the existing AcroForm fields and pages. Returns true, or false when there was no XFA |
final form = sdk.open(await File(taxFormPath).readAsBytes());
form.xfaImportData(await File(dataXmlPath).readAsString()); // XML
form.xfaToAcroform(); // now fill it like any AcroForm
await File(acroformPath).writeAsBytes(form.save());
form.dispose();AcroForm fields made from XFA are named by their SOM path (form1[0].item[1].qty[0]). See
Forms to fill and flatten them.
Convert with a report
The report form of the conversion, nexpdf_xfa_to_acroform_json, has no typed method on
NexPdfDocument yet. Reach it through sdk.invoke, which works on invoke handles (integers), not
NexPdfDocument objects. The options are {"scripts": true, "flatten": false} (null uses these
defaults): scripts runs the form's scripts during the conversion, flatten draws the values
instead of keeping fields.
final h = sdk.invoke('open_bytes', [await File(taxFormPath).readAsBytes()]) as int;
final report = jsonDecode(sdk.invoke('xfa_to_acroform_json', [h, '{"scripts":true,"flatten":false}']) as String)
as Map<String, Object?>;
print('${report['fields']} ${report['pages']} ${report['flattened']}');
for (final e in report['validation_errors'] as List) {
print('${e['som']} ${e['test']} ${e['severity']} ${e['message']}');
}
await File(acroformPath).writeAsBytes(base64Decode(sdk.invoke('save_bytes', [h]) as String));
sdk.invoke('close', [h]);After conversion, text fields hold the value they show: the display picture is applied (for
example 1,234.50 or 29 Nov 2026) and stored as the field value, and the form is marked so
that viewers which draw text fields from the value (such as macOS Preview) show the same text as
the appearance. Password fields and values longer than the field's maximum length keep the raw
value. The raw data values stay in the XFA data only until you convert, so call
xfaExportData first if you need them. Static XFA forms filled in place keep raw values.
Scripts
FormCalc (with a built-in interpreter) and JavaScript scripts run against the XFA object model
(xfa.resolveNode, this.rawValue, xfa.host, instanceManager, script objects). The
initialize, calculate (repeated until the values settle) and validate events run when data is
imported and when the form is converted or flattened; conversion also runs ready, docReady,
prePrint, page-area layoutReady and conditional breaks. Failed validations are reported.
nexpdf_xfa_run_scripts_json runs initialize, calculate and validate on their own and stores
the calculated values in the data. It is also reached through invoke:
sdk.invoke('xfa_import_data', [h, '{"form1.item[1].qty":"3"}', 'json']);
final result = jsonDecode(sdk.invoke('xfa_run_scripts_json', [h]) as String) as Map<String, Object?>;
print('${result['data_changed']} ${result['warnings']} ${result['validation_errors']}');Layout and fonts
The layout applies picture clauses (display, edit and data pictures for dates, times, numbers,
currency and text, with built-in locales) and draws rich text, images (embedded, linked and
data-bound), table column widths, right-to-left flow, leaders and trailers, overflow targets,
conditional breaks and occur on page areas. Data binding supports once, none, global and
dataRef with full SOM expressions.
Text is drawn in embedded Unicode fonts, shaped with OpenType and ordered with the Unicode bidi
algorithm. The bundled Liberation fonts cover Latin, Greek and Cyrillic; for CJK, Arabic and other
scripts register a fallback font once per process (TrueType, OpenType or .ttc; the data is
copied):
sdk.invoke('register_fallback_font', [await File(notoSansCjkPath).readAsBytes()]);When no font has the glyphs for some text, a warning says to register one.
Barcode fields are drawn as vector barcodes: QR, Data Matrix, Aztec, PDF417, Code 128 (A, B, C and GS1-128), Code 39, Code 93, EAN-13, EAN-8, UPC-A, UPC-E, Interleaved 2 of 5, Codabar and Telepen.
Limits
- Other barcode types (postal codes, RSS, MaxiCode and so on) stay text fields, with a warning.
- Host dialogs such as
xfa.host.messageBoxare not shown; they are recorded as warnings.