EvaluateXFAFormCalc
Form fields and XFA
Description
Evaluates an XFA FormCalc script expression against the current XFA datasets and form DOM of the selected document and returns the resulting value as a string
Syntax
Delphi
Function TPDFlib.EvaluateXFAFormCalc(Const Script: WideString): WideString;
Parameters
| Script | The FormCalc expression or multi-statement script to evaluate |
|---|
Return values
| WideString | The evaluated string or formatted numeric result of the final expression in the script, or an empty string if evaluation fails |
|---|
Remarks
The bounded expression subset supports numeric arithmetic including exponent literals, SOM field references and wildcard aggregation, case-sensitive var variables, assignments, if/elseif/else/endif blocks and the implemented arithmetic, statistical, logical and string functions
Newlines separate statements; semicolons and // start comments through the next newline, and newlines inside parentheses permit expression continuation
Bounded loops, functions and lexical blocks
FormCalc supports while, for/upto, for/downto, optional step expressions, foreach over scalar arguments or wildcard values, break, continue, explicit do/end blocks and func/do/endfunc declarations
For loops reevaluate their end expression before each iteration and step expression after each completed or continued body; foreach evaluates its argument list once, and break or continue affects only the innermost loop in the current function
Declarations follow lexical block scope, including shadowed variables and user functions; assignments update the nearest visible variable, function parameters are local scalar values, and the final function expression supplies its return value
Implemented built-in functions take precedence over user declarations with the same name; recursive calls and nested functions retain the declaration environment and contribute to the shared expression and operation budgets
Inactive branches parse their structure without executing writes or repeated loop bodies; user functions are limited to 32 parameters, 32 active calls and 256 visible declarations, with 16384 cumulative scopes per script
This remains a bounded scalar calculation profile: arbitrary object methods, executable code generation and unsupported built-ins are explicit failures; zero-step or nonterminating loops exhaust the shared budget and roll back all staged writes
Empty successful values are distinguished from failure by LastErrorCode and GetFormScriptDiagnostics
Evaluation stages changes on a private DOM; only a successful evaluation commits assignments and synchronises matching text or choice AcroForm values, so an unsupported or malformed later expression cannot leave earlier writes behind
Fixed limits are 65536 script characters, 16384 tokens or expanded values per list, 100000 shared token/expression operations per calculation batch, 64 expression nesting levels, 1024 local variables or calculated fields, 1048576 characters per string and 16777216 cumulative intermediate-value characters
Numeric operands and results must be finite with absolute value at most 10^15; integer string lengths are bounded before allocation and rounding precision is limited to -8 through 8
Errors use the same 703 and 706 through 709 codes as ExecuteXFAFormCalculations; FormCalc uses its fixed bounds independently of SetFormScriptLimits
See also
ExecuteXFAFormCalculations, GetXFAFormFieldValue, SetXFAFormFieldValue