How to Set a Global Variable to the Current PDF Document in Acrobat
Question details
The user wants to know how to properly reference the currently open PDF document by assigning the document object to a global variable using Acrobat JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Writing JavaScript code within Adobe Acrobat to automate tasks or manipulate the active PDF file across different scopes.
- Observed behavior
- The user needs the correct JavaScript syntax to define variables for both local file-level scope and application-level cross-file global scope.
Before modifying your PDF with JavaScript, ensure you have access to the Acrobat JavaScript console or the document-level scripting environment.
Assigning the Document Object to a Variable in Acrobat JavaScript
Use the 'this' keyword to reference the current document, and choose between standard variables for file-scope or the 'global' object for cross-file scope.
In Acrobat JavaScript, the 'this' object generally points to the document object when executing scripts within a PDF. Depending on where you need to access this reference, you can assign it to a standard variable or an application-level global variable.
If you only need to use the variable within the current script or throughout the single open file, declare it by typing: `current_doc = this;` in your script editor.
If you need the variable to be accessible from completely different scripts, other open PDF files, or application-level processes, assign it to the global object by typing: `global.myDoc = this;`

Edit and Manage PDF Documents Easily with WPS Office
While Acrobat JavaScript offers complex programmatic control, you can quickly achieve most document management and form-filling goals visually using WPS Office's built-in PDF tools.
- 1. Open your PDF in WPS: Launch WPS Office and click on 'Open' to browse and select your PDF file.
- 2. Access PDF Editing Tools: Navigate to the 'Edit' tab to directly modify text, images, or document properties visually.
- 3. Interact with Forms: Use the 'Form' tab to fill out interactive PDF forms, add signatures, or insert new form fields.
- 4. Save or Export: Click 'Menu' and select 'Save As' to securely save your document, or export it to other fully compatible Office formats.

Frequently Asked Questions
What does the 'this' keyword refer to in Acrobat JavaScript?
In document-level scripts, bookmark actions, and PDF form fields, the 'this' keyword refers to the current Document object. It allows you to access document properties and execute methods directly.
How long does a global variable persist in Adobe Acrobat?
A true global variable attached to the `global` object persists for the duration of the Acrobat application session. It is completely cleared from memory when you close the Acrobat application.
Can I use 'current_doc = this' in a folder-level script?
In folder-level scripts, 'this' typically refers to the application object (app) rather than the active document. To reference a document in a folder-level script, you generally need to pass the document object explicitly or use 'app.activeDocs' to find the target document.




