ExecuteXFAFormCalculations
Form fields and XFA
Description
Executes supported FormCalc and JavaScript calculation scripts declared in the XFA template of the selected document, updating both XFA dataset values and synchronised AcroForm fields
Syntax
Delphi
Function TPDFlib.ExecuteXFAFormCalculations: Integer;
Return values
Returns the number of distinct dataset scalar values changed by the committed calculation batch, also counting matching AcroForm field names repaired from stale values; repeated passes and duplicate widgets do not multiply the count
Zero means no calculations or committed value changes, or a rejected batch; inspect LastErrorCode and GetFormScriptDiagnostics to distinguish them
Remarks
Calculations run on a private DOM and repeat in document order until values stop changing, with at most the number of calculation fields plus one passes, capped at 64; dependency cycles are rejected even if initial values already agree
FormCalc loops, scoped user functions and nested calls use the same private calculation data and dependency graph; all iterations and calls contribute to the shared batch budget
FormCalc scripts use application/x-formcalc or an absent content type; unsupported calculation languages, an unsupported function or control-flow construct, malformed input, ambiguous targets and exhausted budgets reject the complete batch
Field calculations address existing dataset nodes through their template path or explicit bind ref; relative reads and writes first use the calculation field's data parent, and .rawValue or .value suffixes address the same value
Successful batches synchronise matching text and choice AcroForm fields and defer appearance regeneration; parent fields are containers and are skipped, while a differing matching field of another type rejects synchronisation before data is committed
Rejected batches preserve XFA data and matching AcroForm values; error 706 means unsupported functionality, 707 means a dependency cycle, 708 means invalid input or synchronisation, and 709 means a work limit or nonconvergence; no form uses 703
GetFormScriptDiagnostics retains the compatible engine: xfa-formcalc label and adds profile: bounded-xfa-calculation, status, successful pass/operation/change counts, or a failure code and message with committed: false; the fixed FormCalc bounds are described by EvaluateXFAFormCalc
Each calculation target must resolve to exactly one dataset node; ambiguous implicit targets or wildcard bindings reject the entire batch with error 708 before any calculations execute, while an explicit occurrence can select one node
Bounded JavaScript calculations
Calculation scripts also accept application/x-javascript, application/javascript and text/javascript through the existing bounded expression engine; FormCalc and JavaScript share the same private datasets, read-dependency graph, convergence checks and final AcroForm synchronisation
Supported JavaScript includes var declarations, scalar assignments, arithmetic, string concatenation, comparisons, if/else statements, conditional expressions, short-circuit logical operators, Number, String, decimal-prefix parseFloat, qualified Math operations and the engine's bounded scalar string or number methods
XFA fields are accessed through this.rawValue, relative or qualified field paths, $data paths, xfa.form paths, and xfa.resolveNode or this.resolveNode with a string SOM path; selectors such as indexed occurrences belong inside resolveNode strings
References follow explicit template data bindings and lexical form-container lookup; numericEdit or integer/decimal/float value declarations expose numeric rawValue values, text fields expose strings, and an empty raw value is null
The XFA value property denotes a node and is not an alias for rawValue; object-valued properties, multivalue containers, incompatible scalar types sharing one data binding and ambiguous or missing references are outside the qualified scalar profile
The final executed expression or assignment supplies the calculation result; short-circuit and inactive conditional branches do not read fields or contribute dependencies, and reading a scalar after assigning it in the same script uses its staged value
JavaScript retains strict type comparisons, operand-valued logical operators, decimal-prefix parseFloat, null/string conversion and rounding towards positive infinity at half-integer Math.round boundaries; nonfinite or out-of-range numeric results are invalid
JavaScript uses the shared 100000-operation and 16777216-value-character batch budgets, 65536 source characters, 64 expression levels, 1024 variables, 1048576 characters per string, and at most 4096 cataloged data nodes or form bindings
Functions and loops in JavaScript, let/const block bindings, arbitrary object methods, dynamic code execution, host actions and external resources remain unsupported; use the bounded FormCalc loop and function profile where those supported constructs are required
An active Math.pow call without an exponent rejects the batch with error 708; scalar string indexOf returns zero for an empty search string
Scalar string charAt truncates fractional positions towards zero before checking bounds, including negative fractions between -1 and 0; the fixed numeric-result limits still apply
See also
EvaluateXFAFormCalc, GetXFAFormFieldValue, SetXFAFormFieldValue