PDF Editing Issues

How to Add a Document Will Close Action using Acrobat JavaScript

Muhammad TalhaMuhammad Talha Nov 5, 2026 869 views

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.

How to Add a Document Will Close Action using Acrobat JavaScript
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 you start

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.

Solution 1Recommended

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.

1
Define the Initialization Script

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;

2
Open Action Wizard

In Adobe Acrobat Pro, navigate to the Tools menu and select 'Action Wizard'. Click 'New Action' to start configuring your batch process.

3
Add a JavaScript Execution Step

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.

4
Input the setAction Script

Click 'Specify Settings' on the Execute JavaScript step and enter the following code: this.setAction("WillClose", "app.runtimeHighlight = highlightPref;");

5
Test on Sample Documents

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.

Use the Document setAction Method in Action Wizard
Trust and Security: Document-level JavaScript actions require the PDF files to be trusted and the recipient's Acrobat application to have JavaScript enabled in its preferences. Otherwise, the WillClose event will be blocked.
Free Microsoft Office alternative

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. 1. Download and Install: Visit the WPS Office website to download the free suite and install it on your device.
  2. 2. Open Your PDF: Launch WPS Office and drag your PDF file directly into the workspace to open the built-in PDF editor.
  3. 3. Edit Easily: Use the intuitive toolbar to modify text, add annotations, or convert the PDF into an editable Word document with one click.
Seamlessly edit, merge, split, and sign PDF documents without needing advanced JavaScript coding.High compatibility with Microsoft Office formats, enabling effortless switching between Word, Excel, PowerPoint, and PDF.Lightweight software that installs quickly, opens fast, and uses minimal system resources.
microsoft office alternative - wps office

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.