|
Registers a document-level named JavaScript function in the Catalog /Names /JavaScript name tree (ISO 32000-1 §7.7.4.4 + §12.6.4.16). AcroForm widget actions, document-open actions, and other JavaScript bodies can invoke the registered function by name through the viewer’s JavaScript engine (Adobe Acrobat, Foxit, and other compatible viewers).
Delphi syntax:
procedure RegisterDocumentJavaScript(const Name, Body: AnsiString);
Parameters
Name is the unique key under which the JS source is stored in the name tree. Case-sensitive per PDF 1.7 §7.9.6. Empty Name raises.
Body is the raw JavaScript source. HotPDF does not parse or validate the JS – the caller is responsible for syntactic correctness. Typical callers pass a complete function declaration (function GreetUser() { app.alert("hello"); }). Empty Body raises.
Description
Each call adds a (key, indirect dict) pair to the Catalog /Names /JavaScript name tree at EndDoc time. Each registered Body becomes its own indirect << /S /JavaScript /JS (body) >> dict, ready to be referenced by name from any other action or JS context. Useful for sharing a library of validation, formatting, or calculation helpers across many widgets without duplicating the source in each /A action.
Registering the same Name twice raises so callers do not silently lose their first definition.
Compliance gates
Raises under any non-empty PDFACompliance setting (ISO 19005-1 §6.6.1 / ISO 19005-2 §6.5.1 / ISO 19005-3 §6.5.1 prohibit all JavaScript actions across PDF/A levels). Raises under any non-empty PDFXCompliance setting (ISO 15930 prepress workflows reject interactive scripting). Automatically bumps the document Version to PDF 1.3 when needed; refuses under StrictVersionLock if the chosen version pre-dates PDF 1.3.
Example
PDF := THotPDF.Create(nil);
PDF.FileName := 'form.pdf';
PDF.BeginDoc;
PDF.RegisterDocumentJavaScript('GreetUser',
'function GreetUser() { app.alert("Hello"); }');
PDF.RegisterDocumentJavaScript('ValidateNumber',
'function ValidateNumber(v) { if (isNaN(v)) app.alert("Not a number"); }');
// ... AcroForm widget /A action can then call: ValidateNumber(this.value);
PDF.EndDoc;
Availability
HotPDF v2.119.14. PDF 1.3+ feature; HotPDF auto-bumps the document Version unless StrictVersionLock is set.
See also: AcroForm Support, THPDFActionScriptType
|