How to Add a Document Will Close Action using Acrobat JavaScript
Question details
The user needs to use Action Wizard to apply a document-level JavaScript action that restores the runtime highlight preference when a PDF closes, across multiple files.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating the addition of a specific JavaScript command that triggers on the WillClose event across a large batch of PDF documents.
- Observed behavior
- Requires the exact JavaScript syntax and method to programmatically assign the highlight preference restoration script to the WillClose event.
Before applying document-level scripts via the Action Wizard, ensure your distribution workflow permits JavaScript execution and always test your automation on a backup copy of your PDF folder.
Use the Document setAction Method in Action Wizard
Assign custom JavaScript code to the WillClose event programmatically so that original application settings are restored upon exiting the document.
When changing application-level configurations such as runtime highlights, it is a best practice to capture the user's initial preference upon opening the document and restore it when the document closes. You can apply this automatically to a batch of PDFs using the setAction method within Acrobat's Action Wizard.
Ensure you have a separate document-level initialization script that saves the user's current highlight preference into a global variable, for example: highlightPref = app.runtimeHighlight;
In Adobe Acrobat Pro, navigate to the Tools menu and select 'Action Wizard'. Click 'New Action' to start configuring your batch process.
From the left pane under 'More Tools', select 'Execute JavaScript' and add it to your action steps on the right. Uncheck the 'Prompt user' box so it runs automatically.
Click 'Specify Settings' on the Execute JavaScript step and enter the following code: this.setAction("WillClose", "app.runtimeHighlight = highlightPref;");
Save your action and run it on a small folder containing two or three test PDFs. Open these PDFs, verify the initialization script runs, and then close them to confirm the WillClose action restores the highlights properly.

Need a Simpler Way to Manage PDFs? Try WPS Office
While advanced JavaScript batch actions in Adobe Acrobat require coding and complex configuration, many users just need a reliable, lightweight tool for everyday PDF editing, merging, and signing. WPS Office provides a comprehensive, user-friendly PDF editor alongside its standard suite, making it the perfect free alternative for your daily workflow.
- 1. Download and Install: Visit the WPS Office website to download the free suite and install it on your device.
- 2. Open Your PDF: Launch WPS Office and drag your PDF file directly into the workspace to open the built-in PDF editor.
- 3. Edit Easily: Use the intuitive toolbar to modify text, add annotations, or convert the PDF into an editable Word document with one click.

Frequently Asked Questions
Why isn't my document-level JavaScript running when the PDF closes?
Your script might not execute if the PDF viewer's preferences have JavaScript disabled for security reasons, or if the document is not opened from a trusted location. Ensure JavaScript is enabled in your Adobe Acrobat preferences.
Can I test my WillClose action on a single document before batch processing?
Yes, it is highly recommended to test the this.setAction method on one or two copies of your documents first via the document properties or JavaScript console. This ensures the script captures and restores the preferences correctly.
What happens if I don't define the highlight preference variable first?
If the highlightPref variable is not previously defined via an initialization script when the document opens, the WillClose action will fail to execute properly because the variable will be undefined, resulting in a JavaScript runtime error.
Is the Action Wizard available in the free Acrobat Reader?
No, the Action Wizard is an advanced feature available only in Adobe Acrobat Pro. However, once the JavaScript is embedded into the documents using Acrobat Pro, the WillClose action will still execute in Acrobat Reader, provided JavaScript is enabled.




