How to Create an If-Then JavaScript Calculation in Adobe Acrobat PDF Forms
Question details
The user needs to set up a custom calculation script for a 'Total' field in a PDF form using conditional logic.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Configuring advanced form field calculations where the final sum depends on whether one field's value is strictly greater than another.
- Observed behavior
- The Total field must dynamically calculate as FieldA + FieldC if FieldB is greater than FieldC; otherwise, it must calculate as FieldA + FieldB.
Ensure you have the exact field names used in your PDF form, as JavaScript is highly case-sensitive and incorrect field names will cause the calculation to fail.
Apply Custom JavaScript for If-Then Calculation
Use Adobe Acrobat's custom calculation script feature to define specific conditional logic for the Total field.
By utilizing the 'Custom calculation script' option in the field properties, you can write JavaScript to evaluate the values of other fields and output the correct sum based on your specific rules.
Open your PDF document in Adobe Acrobat. Go to 'Tools' and select 'Prepare Form' to enable form field editing.
Right-click on your 'Total' field and select 'Properties' from the context menu.
In the Properties window, click on the 'Calculate' tab at the top. Select the 'Custom calculation script' radio button.
Click the 'Edit' button and paste the following code: var a = Number(this.getField('FieldA').valueAsString); var b = Number(this.getField('FieldB').valueAsString); var c = Number(this.getField('FieldC').valueAsString); if (b > c) event.value = a + c; else event.value = a + b;
Click 'OK' to close the script window, then click 'Close' on the Properties window. Exit Prepare Form mode and test your form by entering numbers into FieldA, FieldB, and FieldC.

Easily Fill and Manage PDF Forms with WPS Office
WPS Office provides a highly compatible and lightweight PDF editor. While setting up complex JavaScript requires specialized authoring, WPS perfectly executes pre-configured calculated PDF forms, allowing you to seamlessly fill, read, and manage your documents.
- 1. Download WPS Office: Install WPS Office Free on your device and launch the application.
- 2. Open Your Interactive PDF: Click 'Open' and select your fillable PDF form that contains the JavaScript calculations.
- 3. Fill in the Data: Navigate through the input fields and enter your data. The pre-set calculations will execute automatically as you type.
- 4. Save and Export: Once completed, use the 'Save' function or the 'Fill & Sign' tools to finalize and securely export your filled document.

Frequently Asked Questions
Why isn't my PDF JavaScript calculation working?
The most common reason is mismatched field names. Double-check your spelling, punctuation, and capitalization in the script against the actual names of FieldA, FieldB, and FieldC. Also, ensure there are no syntax errors in your code.
Can I perform basic math in Acrobat without JavaScript?
Yes. For simple addition, subtraction, multiplication, or averaging without conditions (If-Then statements), you can use the 'Value is the [sum/product/average] of the following fields' option in the Calculate tab.
Are JavaScript calculations supported in all PDF viewers?
Most modern desktop PDF applications like Adobe Acrobat and WPS Office fully support standard JavaScript calculations. However, some basic mobile PDF viewers or built-in web browser viewers may lack support for complex scripts.




