Use Acrobat JavaScript for PDF Form Buttons and Fields
Question details
Learn how to use Acrobat JavaScript to control PDF form fields, calculate values, and validate entries.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- The user is building an interactive PDF form with buttons, calculated totals, and percentage limits.
- Observed behavior
- The form should respond to button actions, calculate values, and alert users when entries are outside the allowed range.
Open the PDF in Adobe Acrobat and confirm that the Prepare a Form tool is available. Plan the field names and check whether the PDF viewer used by recipients supports Acrobat JavaScript.
Use button actions to control PDF form fields
Create named fields and assign button scripts to show, hide, add, or remove fields.
Open All tools > Prepare a form, add a text field and a button, and name the text field Details in its Properties dialog. Field names in scripts must match exactly.
Open the button's Properties > Actions, select Mouse Up as the trigger and Run a JavaScript as the action, then click Add.
In the JavaScript editor, enter `this.getField("Details").display = display.hidden;` to hide the field. Use `display.visible` instead to show it. Save the action and test the button in Preview.
For a separate button action, use `this.addField("Extra", "text", 0, [72, 720, 200, 700]);` to add a text field on the first page. Adjust the rectangle coordinates for the page. To remove it, use `if (this.getField("Extra")) this.removeField("Extra");`.

Calculate totals and validate percentage entries
Use field calculation and validation events to update totals and reject values outside a defined range.
Work with PDF forms and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDF and Microsoft Office files. Its format compatibility makes it easy to move existing documents into WPS, but it does not replace Acrobat's JavaScript form-authoring tools or guarantee that Acrobat scripts will run.

Frequently Asked Questions
Where do I add JavaScript to a PDF button in Acrobat?
In Prepare a form, open the button's Properties > Actions. Select Mouse Up, choose Run a JavaScript, and click Add.
Why doesn't my form script find a field?
Check the field's name in its Properties dialog and make sure the name in `this.getField()` matches it exactly.
Can every PDF viewer run Acrobat JavaScript?
No. JavaScript support and permissions vary among PDF viewers. Test the form in the viewer your recipients will use, especially if scripts add or remove fields.
How do I stop a user from entering an out-of-range percentage?
Open the field's Properties > Validate and add a custom validation script. Use `app.alert()` to explain the limit and set `event.rc = false` to reject an invalid entry.




