logo
search
PDF Editing Issues

How to Create an If-Then JavaScript Calculation in Adobe Acrobat PDF Forms

Chanuka GeekiyanageChanuka Geekiyanage Oct 23, 2026 868 views

Question details

The user needs to set up a custom calculation script for a 'Total' field in a PDF form using conditional logic.

How to Create an If-Then JavaScript Calculation in PDF Forms
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.
Before you start

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.

Solution 1Recommended

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.

1
Enter Prepare Form Mode

Open your PDF document in Adobe Acrobat. Go to 'Tools' and select 'Prepare Form' to enable form field editing.

2
Open Field Properties

Right-click on your 'Total' field and select 'Properties' from the context menu.

3
Navigate to the Calculate Tab

In the Properties window, click on the 'Calculate' tab at the top. Select the 'Custom calculation script' radio button.

4
Insert the JavaScript Code

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;

5
Save and Test

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.

Apply Custom JavaScript for If-Then Calculation
Calculation Order: If you have multiple calculated fields, ensure the calculation order is correct. You can adjust this by clicking 'More' > 'Clear Form' > 'Set Field Calculation Order' in the Prepare Form menu.
Efficient PDF Form Management

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. 1. Download WPS Office: Install WPS Office Free on your device and launch the application.
  2. 2. Open Your Interactive PDF: Click 'Open' and select your fillable PDF form that contains the JavaScript calculations.
  3. 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. 4. Save and Export: Once completed, use the 'Save' function or the 'Fill & Sign' tools to finalize and securely export your filled document.
Fully compatible with Adobe PDF formats and calculated form fieldsLightweight application with fast rendering on all devicesFree tools to read, sign, and fill forms easilyIntuitive UI that requires no learning curve
microsoft office alternative - wps office

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.