How to Add a Reusable Button to an Acrobat JavaScript Dialog
Question details
The user wants to create a custom JavaScript dialog box in a PDF that includes a button capable of incrementing a specific text field's value repeatedly without closing the dialog.

- Product
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms using document-level JavaScript and custom dialogs.
- Observed behavior
- The goal is to increment a numeric value in a form field using a custom dialog button, while ensuring the dialog remains open for continuous interaction.
Ensure you are using a professional PDF editor that supports document-level JavaScript and the Acrobat JavaScript API, as basic PDF readers cannot execute custom dialog scripts.
Create a Custom Dialog with a Button Callback
Define a custom dialog using Acrobat JavaScript and assign a callback function to the button that updates the text field while keeping the dialog active.
When defining a custom dialog in Acrobat JavaScript, standard buttons often close the dialog automatically upon selection. To bypass this, you can assign a custom function to a specific element. By utilizing a callback function triggered by the button click, you can manipulate form fields without returning an 'OK' or 'Cancel' command, thereby preventing the dialog from closing.
Create a JavaScript object that outlines the dialog properties, specifying elements such as text, input fields, and the custom button.
Inside the dialog object's definition, add a function specifically for your button. Within this function, use `this.getField('YourFieldName')` to retrieve the target text field.
Retrieve the current value of the field, parse it as a number, add your increment amount (e.g., +1), and assign the new value back to the field's `value` property.
Ensure the button's callback function does not return a closing command like `dialog.ok` or `dialog.cancel`. Returning an empty string or leaving the return undefined will keep the dialog active.
Trigger the dialog to appear by calling `app.execDialog(yourDialogObject)` via a document action or a standard PDF form button.

Create and Manage Interactive PDF Forms with WPS Office
While scripting custom JavaScript dialogs requires advanced coding skills, WPS Office provides comprehensive PDF editing tools that allow you to easily create, fill, and manage interactive PDF forms in a highly intuitive interface.
- 1. Open your PDF Document: Launch WPS Office and open your interactive PDF form.
- 2. Access Form Tools: Navigate to the 'Form' tab to view and manage all interactive fields within the document.
- 3. Fill and Edit: Click directly on text fields, checkboxes, or interactive buttons to enter your data or trigger basic actions.
- 4. Save the Document: Once your form is filled or modified, click the Save icon or go to Menu > Save to securely retain your changes.

Frequently Asked Questions
Why does my JavaScript dialog close immediately after clicking a button?
By default, standard dialog buttons (like OK or Cancel) will close the dialog automatically. To prevent this, you must assign a custom callback function to the button that executes your code without returning a close command to the dialog handler.
Can I use a JavaScript dialog button to update multiple PDF fields at once?
Yes. Within your button's callback function, you can retrieve and assign new values to multiple field objects sequentially using the `this.getField()` method before the function completes.
Do custom JavaScript dialogs work in all PDF readers?
No, custom JavaScript dialogs typically require advanced PDF applications. Many web browsers, mobile viewers, and basic PDF readers do not support the `app.execDialog` API required to render these dialogs.
How do I trigger a custom JavaScript dialog to open in my PDF?
You can attach the `app.execDialog()` script to a document-level action, a page open event, or to the 'Mouse Up' action of a standard form button placed directly on the PDF page.




