How to Define Global Variables in Acrobat PDF JavaScript
Question details
The user wants to create document-level JavaScript variables that can be reused by field-level scripts throughout an interactive Acrobat PDF form.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Developing an interactive PDF form and needing to share data or functions across multiple form fields without redefining them.
- Observed behavior
- Needs a method to declare shared variables at the document scope so they can be referenced globally by any field script within the same PDF document.
Ensure you have Adobe Acrobat Pro or a compatible advanced PDF editor installed, as standard Adobe Reader does not allow the creation of document-level JavaScripts. Make sure your interactive PDF form is opened in editing mode.
Create Document-Level JavaScript Variables in Acrobat
Define shared variables at the document scope using the Document JavaScripts tool so they can be referenced by any field script in the same PDF.
By defining variables at the document level, you ensure they are initialized when the PDF opens and remain accessible to all form fields globally. This prevents script redundancy and makes maintaining your interactive forms much easier.
Launch your PDF in Adobe Acrobat and navigate to the 'Tools' menu. Select 'Prepare Form' to enter form editing mode.
In the right-hand pane or top toolbar, click on 'More' (the wrench or expanded menu icon) and select 'Document JavaScripts'. Alternatively, press 'Shift+D' on your keyboard.
In the JavaScript Functions dialog box, type a name for your script in the 'Script Name' field and click 'Add'.
In the script editor window, delete the default function template. Declare your shared variables outside of any functions (e.g., var myGlobalVariable = 'Hello';) and click 'OK' to save.

Manage and Fill PDF Forms Easily with WPS Office
While creating document-level JavaScript requires specialized authoring tools, WPS Office provides an incredibly user-friendly built-in PDF editor. It is perfect for filling out interactive forms, editing text, and seamlessly managing standard PDF documents.
- 1. Download WPS Office: Download and install WPS Office from the official website.
- 2. Open Your PDF Document: Launch the application, click on 'Open', and select your PDF file.
- 3. Access PDF Tools: Navigate to the 'PDF' tab to use form-filling, highlighting, and editing features.
- 4. Save Changes: After filling in your required fields or making annotations, click 'Save' to securely store your document.

Frequently Asked Questions
Can I use document-level JavaScript variables across different PDF files?
No, document-level variables are strictly scoped to the specific PDF document they are created in. To share variables across multiple different PDF files, you would need to utilize folder-level scripts or import external data.
Why are my global variables returning undefined in field scripts?
This usually happens if the variable is accidentally declared inside a function within the document-level script. Ensure your variables are declared outside of all function blocks so they load globally when the document is initialized.
Where do I write the field-level scripts to use these global variables?
Right-click the specific form field in Acrobat, select 'Properties', and navigate to the 'Calculate', 'Validate', or 'Format' tabs. You can enter custom JavaScript there that references the global variables you defined.




