How to Run JavaScript When a PDF Document Closes
Question details
The user needs to execute a JavaScript function automatically when a PDF document is closed to restore the original application settings that were modified upon opening.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Managing document-level JavaScript to control application settings across the lifecycle of opening and closing a PDF file.
- Observed behavior
- Application-level settings modified by a PDF script remain changed after the document closes unless explicitly restored via a Document Will Close event trigger.
Ensure your PDF editor supports document-level JavaScript and that you have the necessary permissions to edit the document's interactive elements and action scripts.
Using Document Actions to Trigger JavaScript on Close
Define your restore functions in a document-level script and bind the execution to the Document Will Close event.
To prevent unintentionally altering a user's application preferences, it is best practice to store the initial state of a setting (like runtimeHighlight) in a variable when the document opens, and restore that exact variable when it closes.
Open your PDF editor and navigate to Tools > JavaScript > Document JavaScripts. Add a new script defining your variables and functions. For example, declare 'var originalRuntimeHighlight = app.runtimeHighlight;' and create a function 'CloseDocument()' that restores this value.
Go to Tools > JavaScript > Document Actions to view available document lifecycle events for the current PDF.
Select the 'Document Will Close' event from the list, click Edit, and input your function call (e.g., 'CloseDocument();'). Click OK to save the action.

Edit and Manage PDFs Easily with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to view, edit, annotate, and manage interactive PDF documents efficiently.
- 1. Download and Install: Download WPS Office from the official website and install the suite on your computer.
- 2. Open your PDF: Launch WPS Office and open your PDF document using the built-in WPS PDF tool.
- 3. Edit and Save: Utilize the extensive editing tools to modify text, fill interactive forms, manage pages, and save your document securely.

Frequently Asked Questions
What is the Document Will Close event in PDFs?
The Document Will Close event is a specific trigger in PDF software that executes an assigned JavaScript action or function exactly when the user initiates the closing of the document, but before the file is actually closed.
Why does my JavaScript setting remain changed after the PDF is closed?
If you change an application-level property (like app.runtimeHighlight) when a PDF opens, it affects the entire PDF viewer. You must explicitly restore the original setting using a Document Will Close script, otherwise the modified setting will persist for other documents.
Can I use document-level JavaScript on mobile PDF viewers?
Most mobile PDF viewers have limited or no support for document-level JavaScript and interactive form scripts. It is highly recommended to test your JavaScript actions on desktop versions of PDF editors to ensure complete compatibility.




