PDF Editing Issues

How to Run JavaScript When a PDF Document Closes

Aamir Naveed AkramAamir Naveed Akram Oct 28, 2026 868 views

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.

How to Run JavaScript When a PDF Document Closes
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.
Before you start

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.

Solution 1Recommended

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.

1
Add the Document-Level Script

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.

2
Access Document Actions

Go to Tools > JavaScript > Document Actions to view available document lifecycle events for the current PDF.

3
Bind to Document Will Close

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.

Using Document Actions to Trigger JavaScript on Close
Conditional Execution: Ensure you only toggle properties like runtimeHighlight if they are initially enabled, to avoid reversing a user's explicitly selected preference instead of preserving it.
Powerful PDF Editor

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. 1. Download and Install: Download WPS Office from the official website and install the suite on your computer.
  2. 2. Open your PDF: Launch WPS Office and open your PDF document using the built-in WPS PDF tool.
  3. 3. Edit and Save: Utilize the extensive editing tools to modify text, fill interactive forms, manage pages, and save your document securely.
Comprehensive PDF editing, form filling, and annotation toolsHigh compatibility with standard PDF formats and advanced document elementsLightweight and fast performance for opening large documentsSeamless conversion and high compatibility with Microsoft Office formats (Word, Excel, PowerPoint)
microsoft office alternative - wps office

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.