How to Call a PDF Dialog Function from an Acrobat Tool Button
Question details
The user needs to trigger a custom JavaScript dialog box from a tool button in a PDF without embedding the entire script inside the cExec parameter.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Developing custom tools and interactive elements using JavaScript in PDF documents.
- Observed behavior
- A more maintainable method is required to call dialog functions rather than converting massive scripts into a single string for the tool button's execution parameter.
Ensure that your JavaScript is syntactically correct and that you have the necessary permissions to execute document-level or folder-level scripts in your PDF application. Testing your code in a safe, duplicate PDF file is highly recommended.
Define the Dialog Function at the Document Level
Create a document-level JavaScript function to define your dialog, which keeps the tool button's cExec call clean and easy to maintain.
By placing your complex dialog definition and execution logic inside a dedicated function, you avoid the nightmare of escaping quotes and handling line breaks inside a massive string. You simply call the function by its name when the button is clicked.
Open your PDF document and navigate to the JavaScript tools (e.g., Tools > JavaScript > Document JavaScripts).
Add a new script, give it a recognizable name, and define your dialog and execution logic within a standalone function, such as: function myCustomDialog() { ... }
Click 'OK' or 'Save' to apply the document-level script and ensure there are no syntax errors reported in the console.
Create your tool button script (using app.addToolButton) and set the cExec parameter to simply call your new function as a string: cExec: "myCustomDialog();"
Execute the tool button script to verify that the button appears in the toolbar and successfully opens the dialog box when clicked.

Use a Folder-Level Script for Global Availability
If you want the tool button and dialog to be available across all PDFs you open on your machine, place the script in the application's JavaScript folder.
Edit PDFs and Manage Forms Seamlessly with WPS Office
While complex JavaScript tool button development is specific to certain advanced scenarios, WPS Office provides an incredibly intuitive, built-in PDF editor for creating forms, adding interactive fields, and modifying document content without writing a single line of code.
- 1. Install WPS Office: Download and install WPS Office, then open your PDF document within the application.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to easily insert text fields, checkboxes, and interactive buttons.
- 3. Edit Content Instantly: Switch to the 'Edit' tab to modify existing text, replace images, and rearrange pages with a user-friendly interface.
- 4. Save and Export: Save your interactive PDF directly or export it for seamless sharing with users on any PDF platform.

Frequently Asked Questions
Why is it bad to put the whole dialog script in the cExec string?
Placing large amounts of code in the cExec string makes it extremely difficult to read, debug, and maintain. Quotes and escape characters often cause syntax errors when nested inside a single execution string.
Can I use document-level JavaScripts in any PDF viewer?
While Adobe Acrobat fully supports document-level JavaScripts and custom dialogs, many other standard PDF viewers have limited or no support for complex JS executions. Always test your PDF in the primary viewer your audience will use.
Where do I find the JavaScript console to test my tool button?
In Acrobat, you can open the JavaScript Debugger by pressing Ctrl+J (Windows) or Cmd+J (Mac) to test snippets, view syntax errors, and manually execute your app.addToolButton code.
Does the tool button stay in the PDF or the application?
If you use app.addToolButton via a document-level script, the button appears only when that specific document is opened. If added via a folder-level script, it remains in the application toolbar globally for all documents.




