XFA form tree creation and editing

TXFAWidgetRuntime supports transactional JavaScript creation, cloning and editing of the live form tree through xfa.form and node collections

The live form tree has independent native identities and can be saved as an XDP form packet. Form edits retain separate authored Template content; default resolution can persist internal declaration metadata for stable source associations

Creating and inserting a field

var row = xfa.resolveNode("row[1]");
var field = xfa.form.createNode("field", "additional");
row.nodes.append(field);
field.w = "160pt";
field.h = "24pt";
field.ui.getElement("textEdit");
field.font.typeface = "DejaVu Sans";
field.rawValue = "Additional value";

createNode(className, name, namespaceURI) returns a detached node, with an empty name and the model namespace used when the optional arguments are omitted

The accepted classes include field, subform, draw, area, exclGroup and the property classes recognized by the runtime property schema

Creating a node does not insert it into the form or create a visible widget until it is attached with the required UI and value properties

Created-node property and attribute coherence

Since Version 2.1143.1, script-created ordinary Form nodes expose the same live presence and access values through direct properties and their existing getAttribute/setAttribute methods. A new ordinary node therefore reports its normal visible/open defaults through either surface

Assigning node.presence = "hidden" makes node.getAttribute("presence") return hidden immediately; node.setAttribute("protected", "access") updates node.access through the same original setter. Clones copy the accepted attributes, each write records one mutation, and a failed write does not update the clone attribute snapshot

Native event commit, later events, saved XDP reload and transaction rollback retain the accepted values. This repairs the existing created-node Form surfaces; it does not add generic attribute methods to every original Form object projected from a Template declaration

Recordless caller Form content remains an authoritative materialized model during runtime initialization, preserving additional created or cloned children after save and reload. Data-backed caller composition and explicit record cursors retain automatic merge behavior. Later data edits do not by themselves convert an authoritative manual model into an automatic model; use the existing explicit remerge or model-selection operations when changing that policy

Collection operations and cloning

OperationBehavior
parent.nodes.append(node)Moves a detached or attached node to the end of the parent's live child collection
parent.nodes.insert(node, before)Moves the node immediately before an existing child of the same parent
parent.nodes.remove(node)Detaches a child while retaining its script identity for later reuse
node.clone(true)Returns a detached deep clone with separate native and script identities
node.clone(false)Returns a detached shallow clone without child nodes or a copied field value

Collection order, parent, property lookup and supported SOM resolution reflect successful edits immediately inside the event

References retained by script objects continue to address their own nodes after a committed edit or an event rollback, including detached deep and shallow clones

Removed master subtrees retain their own objects across later events, including named subforms inside a detached pageSet, pageArea or contentArea. They have no current page geometry or live master data scope; removing them does not redirect their references to an attached clone. Detached values stay outside saved live Form content until reattachment, which restores ordinary master binding and persistence

A cycle, a reference child from a different parent or a data-model node passed to a form operation raises an exception before that invalid operation changes the tree

The form model root can contain newly created subforms and fields, but the model root itself cannot be moved or cloned

Repeated scopes and initial instances

Actual form instances retain separate identities even when their authored names are equal, their data contexts differ or no datasets packet exists

Unnamed field and exclusion-group declarations keep their own class-and-occurrence Form value nodes; writing them does not place a value on the parent subform or merge multiple unnamed values. Native value resolution and script writes preserve these values when XDP is saved and reloaded

An empty merge uses the explicit occur.initial count, or occur.min when initial is absent, and an absent max defaults to min

For an empty merge, an initial count outside the minimum and maximum is permitted, including zero initial copies

Nested and unnamed subforms retain their own occurrence paths, and materializing these instances does not synthesize a datasets packet

Ordinary unnamed subforms also retain their own nodes when a value overlay is created before full Form materialization. Same-name fields below different unnamed parents keep separate values through native or script updates and XDP save/reload; unnamed-parent transparency in SOM lookup does not flatten the stored Form structure

Ordinary area and draw overlays preserve unnamed nodes and separate same-name declarations by their sibling occurrence. A same-name subform uses its declaration occurrence when no matching data ancestor supplies an instance index, so unbound siblings retain independent values and properties

These occurrence rules follow XML Forms Architecture 3.3, printed pages 339–345

New-instance defaults come from the current associated Template declaration, independently of edits to an existing Form instance. Manual instances without that source use a persisted declaration snapshot captured on first successful manager access, while current occurrence limits remain live

Caller Form overlays preserve supplied ordinary sibling order, fill matched property defaults and complete omitted Template siblings at stable relative positions. Existing library-authoritative structure remains authoritative; omission from a caller overlay does not itself declare deletion

During automatic data merge, ordinary owners such as areas retain their declaration identity when their children are reconciled. Same-name or unnamed sibling areas receive their own child containers and field bindings; older nodes without declaration metadata fall back to their class, name, namespace and occurrence

Master-page binding also constructs actual repeated subforms below pageSet and pageArea, initializes fresh scalar values from the owning record and preserves one actual data instance across repeated physical page presentations

JavaScript Template attributes

xfa.template exposes the raw Template packet with identities distinct from xfa.form, including when no datasets packet exists. Use the $template or xfa.template SOM prefix to resolve existing Template nodes

Existing Template nodes provide getAttribute(name) and setAttribute(value, name). Use these methods rather than assuming that every convenience property of a generated Form field applies to a Template node

const templateField = xfa.template.resolveNode("main.probe");
templateField.setAttribute("70pt", "w");
xfa.form.remerge();

The setter edits the actual Template packet within the current event transaction. Remerge reconstructs Form from that edited packet; retained Template handles remain valid, while replaced Form handles retain their existing retirement behavior. Accepted attributes persist through following events and XDP save/reload, and an uncaught failure after a native remerge barrier restores the prior transaction state

Navigation includes raw unnamed property nodes, such as xfa.template.main.probe.font.getAttribute("typeface"). Reading a raw Template script node does not execute a Form script module, and Template subforms do not expose Form instance managers

The exact case-sensitive attribute name xmlns is reserved. Namespace-qualified names remain outside the existing ordinary attribute-name grammar; this API does not author namespace declarations. Invalid XML characters, including NUL and isolated surrogates, are rejected before an attribute edit is recorded

Existing operation, node, value, elapsed-time, VM and journal budgets remain in force. This attribute profile is complemented by Template structural editing; bounded XML import/export is described below

JavaScript Template structure

xfa.template.createNode(className, name, namespaceURI) creates a detached raw Template node. Omitted or empty namespace parameters use the current Template model namespace under the library's creation policy; an explicit nonempty URI is preserved

Use node.clone() or node.clone(true) for a deep copy, and node.clone(false) for a shallow copy with the node's own authored attributes and value. Native deep cloning preserves raw descendant XML, including mixed text, CDATA, comments, processing instructions and inherited namespace bindings

The delivered compatibility catalog accepts115 raw Template class names. Catalog membership does not establish complete class behavior or parent/child schema validation. node.ns reports the actual namespace; supported scalar and raw script nodes expose direct text through value

Clones receive independent runtime declaration provenance. Only the exact internal automatic-merge declaration attribute is cleared from copies before materialization; the source and same-local-name attributes in other namespaces remain unchanged

const model = xfa.template;
const field = model.createNode("field", "additional");
field.setAttribute("100pt", "w");
field.setAttribute("20pt", "h");
const ui = model.createNode("ui");
ui.nodes.append(model.createNode("textEdit"));
field.nodes.append(ui);
const value = model.createNode("value");
const text = model.createNode("text");
text.value = "Created in the Template model";
value.nodes.append(text);
field.nodes.append(value);
model.main.nodes.append(field);
xfa.form.remerge();

Live child lists support append(node), insert(node, before) and remove(node). A move updates parent order, while removal detaches the same node without retiring its Template handle. Detached nodes remain editable and can be reattached during later events

Template operations use a separate native structural graph. Cross-model operands, cycles, invalid insertion references and movement of the live packet root are rejected; a separate detached root clone retains its own identity and does not replace the live packet

Remerge reconstructs native Form from the edited attached Template. Retained Template identities, including registered detached nodes, survive remerge and transaction rollback; replaced Form handles retain their retirement behavior

Saved XDP contains the attached packet tree. Unattached nodes and JavaScript globals retained in the current runtime are not added to a newly loaded document merely because that XDP was saved

Structural changes participate in existing node, XML-byte, depth, operation and event transaction limits. A failure after native remerge restores both accepted attached XML and detached graph state. Full XFA parent/child schema validation remains separate; bounded XML import/export is described below

When MaxValueBytes is zero, the existing bounded fallback is retained. Ordinary positive limits and the native16MiB command ceiling remain enforced; negative configurations are not covered by this acceptance profile

JavaScript Template XML

Raw Template nodes provide saveXML() and saveXML("pretty") to serialize their current XML fragment, including authored attributes and mixed content

loadXML(source, ignoreRoot = true, overwrite = false) imports bounded XML into the target's content while preserving the target's identity, class, name, namespace and attributes. The default appends the incoming root's children; ignoreRoot=false includes the incoming root itself, and overwrite=true replaces the previous content

const text = xfa.template.main.probe.value.text;
text.loadXML("<wrapper>Updated default</wrapper>", true, true);
xfa.form.remerge();

Append preserves existing child handles. Overwrite retires replaced Template descendant handles; a replacement with the same name receives its own identity. Independently detached nodes remain editable across events and may be reattached with the existing live child lists

XML import preserves literal expanded names, including explicit empty namespaces, inherited prefixes used only by attributes, and descendant prefix shadowing. Mixed text, CDATA, comments and processing instructions retain their order. Imported nodes receive independent internal declaration provenance while authored attributes in other namespaces are preserved

When an existing Form representation still shares Template backing nodes, XML import first isolates the affected native Form state while preserving its published script identities. Existing Form values and property handles remain usable until explicit remerge; Form-only writes do not redefine the imported Template content

Accepted attached XML survives following events and XDP save/reload. Uncaught failure after a native remerge barrier restores accepted XML, identities and retirement state. Malformed XML and existing node, depth, byte, operation and elapsed-time limits reject the transaction without publishing a partial import

The bounded XML methods do not establish complete parent/child schema validation, execution semantics for every imported class or universal dynamic XFA compatibility

Explicit reconstruction

Explicit Form remerge uses TXFAWidgetRuntime.Remerge or synchronous xfa.form.remerge() to rebuild from the current Template and datasets, discarding Form-only edits and creating independent node identities

Existing native UpdateDocument callbacks can edit the actual Template packet transactionally before remerge, with saved/reloaded persistence and native failure rollback. The JavaScript Template attribute methods provide a separate event-transaction surface for existing nodes

Resolve fresh Form references after reconstruction; retired Form handles do not become aliases of replacement nodes, including nodes created with the same name before and after the call. Successful reconstruction is saved through SaveToBytes, while an uncaught failure restores the prior transaction state

Commit, rollback and output

A successful event applies its structural journal, rebuilds native widgets and makes the edited form available to SaveToBytes and automatic native PDF creation

An uncaught script exception, native commit failure or exceeded event budget restores the original document, widget identities and retained script state, so a later event can retry

The existing operation, node, depth, byte, widget and elapsed-time budgets also apply to structural preparation and commit

Saved form instances can be reloaded and passed to HPDFXFAWriteHeadlessPDF to create real PDF fields and appearance streams with supplied font bytes

Scope

These APIs edit the live form model and its supported property schema

Unnamed fields also appear in interactive widget enumeration with an empty public name. Their independent values can be edited through widget IDs, native APIs or script class selectors and persist through XDP save/reload

Value overlays retain unnamed pageSet, pageArea and contentArea ancestors and their sibling occurrences, so master-page field updates remain under the correct page model after save/reload

Complete choice subform-set processing, the full XFA class schema and arbitrary binding selector combinations remain separate capabilities