How to Run Custom JavaScript from an Acrobat PDF Button
Question details
The user wants to know how to trigger and execute custom JavaScript code using a button within a PDF form.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Adding interactive scripts to a PDF form to manipulate field values when a user clicks a button.
- Observed behavior
- The user needs to correctly reference field objects within the Actions tab of a button's properties to read or write data.
Ensure you have Adobe Acrobat Pro or a compatible PDF editor with advanced form editing capabilities, and a basic understanding of Acrobat JavaScript API syntax.
Configure Custom JavaScript in Button Properties
Use the Actions tab in the PDF button's properties to add and execute custom JavaScript code using the Acrobat API.
In Adobe Acrobat, buttons can be configured to perform a variety of actions, including running complex JavaScript sequences. To modify or read form fields accurately, you must utilize the specific Acrobat JavaScript API, referencing field objects with the getField method rather than standard web JavaScript functions.
Open your PDF form in Adobe Acrobat. Enter 'Prepare Form' mode from the right-hand tool pane, right-click the target button, and select 'Properties'.
In the Button Properties dialog box, click on the 'Actions' tab to configure the interactive behavior of your button.
In the 'Select Action' dropdown menu, choose 'Run a JavaScript', and then click the 'Add...' button to open the JavaScript Editor.
In the JavaScript Editor window, input your custom code. To read a field value and assign it to another, use code like: var target = this.getField('Text1'); target.value = this.getField('Mark1').valueAsString;.
Click 'OK' to save your script and close the editor. Close the Button Properties window, exit Form Editing mode, and click the button to test your JavaScript.

Manage and Fill PDF Forms Easily with WPS Office
WPS Office features a powerful PDF editor that allows you to effortlessly read, fill, and modify interactive PDF forms. Try it as a lightweight, fast, and highly compatible solution for your daily document management.
- 1. Open Your Interactive PDF: Launch WPS Office and open your PDF document containing the interactive forms and buttons.
- 2. Access Form Controls: Navigate to the PDF editor interface where you can view and interact with text fields, checkboxes, and buttons natively.
- 3. Fill and Save: Click the form buttons to execute their embedded actions or enter your data, then click 'Save' to preserve your inputs securely.

Frequently Asked Questions
Why isn't my PDF button JavaScript executing?
Ensure that JavaScript execution is enabled in your PDF reader's security preferences. In Adobe Acrobat, go to Edit > Preferences > JavaScript and verify that 'Enable Acrobat JavaScript' is checked.
Can I use standard web JavaScript in a PDF form?
No, PDF JavaScript uses a specialized API developed specifically for PDFs (Acrobat JavaScript API). Standard web browser objects like document.getElementById or window are not supported; you must use native objects like this.getField().
How do I read the value of a text field using JavaScript in a PDF?
You can read a text field's value by assigning it to a variable using the syntax: var myValue = this.getField('FieldName').value;. Ensure the field name in the script matches the exact name of the form field.
Are JavaScript calculations automatic in PDF forms?
Scripts added through the Calculation tab run automatically whenever associated field values change. However, scripts added to a button's Actions tab require a specific trigger event, such as a user clicking the button (Mouse Up).




