How to Run Document-Level JavaScript on PDF Open in Adobe Acrobat
Question details
The user wants to automatically execute document-level JavaScript (e.g., for auto-filling fields or retrieving metadata) as soon as a PDF file is opened in Adobe Acrobat.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Adding document-level JavaScript to a PDF document to automate specific tasks upon file opening.
- Observed behavior
- The defined document-level scripts and functions fail to execute automatically when the user opens the PDF file.
Ensure you have Adobe Acrobat Pro installed, as Adobe Acrobat Reader does not allow the creation or editing of document-level JavaScript. Always back up your PDF file before modifying its background scripts.
Trigger the JavaScript Function on PDF Startup
To make document-level scripts run automatically when the PDF opens, you must explicitly call the function outside of its definition block and ensure no JavaScript reserved keywords are used.
In Adobe Acrobat, defining a function at the document level makes it available throughout the PDF, but it does not run it automatically. To trigger the code upon opening the document, the function must be explicitly called at the root level of the script. Additionally, naming a function with a reserved built-in object name (like 'Number' or 'String') will cause silent execution failures.
In Adobe Acrobat, navigate to 'Tools', select 'JavaScript', and then click on 'Document JavaScripts'.
Select the script you previously created from the list and click the 'Edit' button to open the JavaScript Editor.
Check your custom function names. If you named a function using a reserved JavaScript object or keyword (e.g., 'Number', 'Date', 'Math'), rename it to a unique, descriptive identifier (e.g., 'autoFillFormFields').
Scroll to the very end of your script block. Entirely outside of any enclosing function wrappers '{ }', type the name of your function followed by parentheses (e.g., 'autoFillFormFields();'). This explicitly commands Acrobat to run the function when the script loads at startup.
Click 'OK' to close the editor. Save the PDF document, close it completely, and reopen the file to verify that your script now executes automatically upon opening.

Manage and Fill PDFs Easily with WPS Office
If you are struggling with complex JavaScript errors and advanced coding in Adobe Acrobat, consider WPS Office. It provides a lightweight, user-friendly PDF editor that easily handles form filling, annotations, and formatting without requiring complex background scripts.
- 1. Download WPS Office: Visit the official WPS Office website to download and install the free suite.
- 2. Open your PDF: Launch WPS PDF and open your document to view or edit it instantly.
- 3. Fill forms without code: Navigate to the 'Form' tab to interact with PDF fields directly without worrying about backend JavaScript execution.

Frequently Asked Questions
Why is my PDF JavaScript not running in web browsers or other PDF viewers?
Many third-party PDF viewers and web browsers (like Chrome, Edge, or Apple Preview) have limited or zero support for Adobe's proprietary Acrobat JavaScript API. For scripts to execute reliably, the end user generally needs to open the document in Adobe Acrobat or Adobe Acrobat Reader.
How do I access the JavaScript debugger in Adobe Acrobat?
You can open the JavaScript Debugger in Adobe Acrobat by pressing Ctrl + J on Windows or Cmd + J on a Mac. This interactive console allows you to test code snippets, check variable states, and view error messages generated by your document.
Can I run a JavaScript only when a specific page opens instead of the whole document?
Yes. Instead of using Document-level JavaScript, you can use Page Actions. Open the Page Thumbnails panel, right-click the specific page, select 'Page Properties', go to the 'Actions' tab, choose 'Page Open' as the trigger, and select 'Run a JavaScript' as the action.
What are JavaScript reserved words and why do they cause errors?
Reserved words are keywords set aside by the JavaScript language for its own syntax and built-in objects (such as function, return, var, Number, String, Date). Using these as names for your own custom functions or variables creates a conflict with the core language, preventing your script from executing properly.




