Skip to Content
NexPDF

XFA forms

Read XFA forms in a .NET MAUI app, export and import their data as XML or JSON, and convert them to AcroForm or flatten them so the PdfView shows them.

English

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. 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. PdfView shows the PDF pages of the file, which for a dynamic form are often only a "Please wait" page. Convert the form to show it.

Inspect a form

using System.Text.Json.Nodes;

var doc = view.Document!;
var info = JsonNode.Parse(doc.XfaInfoJson())!;
if (info["has_xfa"]!.GetValue<bool>() && info["dynamic"]!.GetValue<bool>())
{
    await Task.Run(() => doc.XfaToAcroform());     // lays the form out into pages
    await view.ReloadAsync();
}

The result has has_xfa, dynamic, field_count, acroform_field_count, data_value_count and the list of XDP packets.

Export and import data

XfaExportData(format) returns the data as xml (the default) or flat json ({"path": "value"}); it throws NexPdfException when the document has no XFA. XfaImportData(data, format) sets values and returns how many it imported:

var values = JsonNode.Parse(doc.XfaExportData("json"))!;
doc.XfaImportData("""{"form1.customer.name":"Ana Silva"}""", "json");
await view.ReloadAsync();

Convert, flatten or remove

CallResult
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 1, or 0 when there was no XFA

Call ReloadAsync() after each. Fields made from XFA are named by their SOM path (form1[0].item[1].qty[0]); fill them as shown in Forms.

Scripts and options

FormCalc and JavaScript scripts run against an XFA object model (xfa.resolveNode, this.rawValue, xfa.host, instanceManager, script objects). initialize, calculate and validate 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.

XfaToAcroformJson(optionsJson) converts with options (scripts, default true; flatten, default false) and reports flattened, fields, pages, warnings and validation_errors (som, test, severity, message). XfaRunScriptsJson() runs the scripts without converting.

var result = JsonNode.Parse(await Task.Run(() => doc.XfaToAcroformJson("""{"scripts":true}""")))!;
await view.ReloadAsync();
foreach (var e in result["validation_errors"]!.AsArray())
    System.Diagnostics.Debug.WriteLine($"{e!["som"]}: {e["message"]}");

The layout applies picture clauses (dates, times, numbers, currency, text), rich text, images, table column widths, right-to-left flow, leaders and trailers, overflow targets, conditional breaks and occur on page areas. Text is drawn in embedded Unicode fonts with OpenType shaping and bidi; for CJK, Arabic and other scripts the bundled Liberation fonts do not cover, register a font once with NexPdfSdk.RegisterFallbackFont(data) (a warning names the missing glyphs otherwise). Barcode fields (QR, Data Matrix, Aztec, PDF417, Code 128, Code 39, Code 93, EAN, UPC, Interleaved 2 of 5, Codabar, Telepen) are drawn as vector barcodes.

After conversion, text fields keep the value they show (with the display picture applied, such as 1,234.50) as their value, so every viewer shows what the appearance shows. Call XfaExportData() first if you need the raw values.

Limits

  • Barcode types not listed above stay text fields, with a warning.
  • Host dialogs (xfa.host.messageBox) are recorded as warnings, not shown.

More examples are in the .NET XFA guide.

On this page