PDF Editing Issues

How to Create an If-Then Calculation in a Fillable PDF Form

Amos GikundaAmos Gikunda Nov 3, 2026 869 views

Question details

The user needs to set up a conditional calculation in a PDF form to output the lower value between two different mathematical sums.

How to Create an If-Then Calculation in a Fillable PDF Form
Product
PDF Editor
Device & OS
not provided
Scenario
Creating a reimbursement or financial form where the final payout is capped based on a logical comparison of different calculated fields (e.g., paying A+B unless A+C is lower).
Observed behavior
The form needs to automatically calculate and display the sum of A+B if it is less than or equal to A+C, and display the sum of A+C otherwise.
Before you start

Ensure you have access to a PDF editor that supports custom JavaScript calculations for form fields, and verify the exact names of the input fields you intend to use in your formula.

Solution 1Recommended

Use a Custom JavaScript Calculation Script

Apply a custom JavaScript code snippet to the target field's calculation properties to perform the If-Then logic.

Standard PDF calculation features usually only cover simple math like sum, product, or average. For conditional logic (If-Then), you must use the Custom Calculation Script option.

When writing the script, ensure that the variables you pull from the form fields are treated as numbers to avoid calculation errors.

1
Open field properties

Right-click the result field where the final amount should appear and select 'Properties' from the context menu.

2
Navigate to the Calculate tab

Go to the 'Calculate' tab within the Properties dialog box to view calculation options.

3
Select custom script

Choose the 'Custom calculation script' option and click the 'Edit' button to open the JavaScript editor.

4
Enter JavaScript code

Enter your conditional logic. For example: `var sum1 = this.getField("A").value + this.getField("B").value; var sum2 = this.getField("A").value + this.getField("C").value; if (sum1 <= sum2) { event.value = sum1; } else { event.value = sum2; }`

5
Save and test

Click 'OK' to save the script, close the properties window, and test your form by entering numbers into fields A, B, and C to verify the correct payout is calculated.

Use a Custom JavaScript Calculation Script
Handling Equal Values: The `<=` (less than or equal to) operator smoothly handles the scenario where both sums are exactly the same by outputting the first sum (A+B), which is functionally identical to A+C in that specific case.
Advanced PDF Form Editing

Create and Calculate Fillable PDF Forms with WPS Office

WPS Office provides a robust set of PDF editing tools, including the ability to create fillable forms, set up field properties, and manage form calculations easily and efficiently.

  1. 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document using the built-in PDF editor.
  2. 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to add text fields, checkboxes, and other interactive elements to your document.
  3. 3. Set field properties: Double-click any form field to open its properties panel, where you can define formatting, data validation, and custom calculation rules.
Create interactive and fillable PDF forms from scratch with ease.Seamlessly compatible with Adobe Acrobat form fields and PDF scripts.Easily edit text, images, and pages within your PDF documents.A lightweight, all-in-one alternative to heavy, expensive PDF editors.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF calculation not updating automatically?

Calculations might not trigger if the field calculation order is incorrect or if the automatic calculation option is disabled in your PDF viewer's preferences. Check the calculation order in your form editing tools to ensure dependent fields are calculated first.

How do I refer to field names with spaces in my JavaScript?

If your form field name contains spaces, you must enclose it in quotes exactly as it appears in the properties. For example: `this.getField("Total Reimbursement").value`.

Can I use mathematical functions like Math.min() instead of an If-Then statement?

Yes, for comparing two values and returning the smaller one, you can simplify the script using `event.value = Math.min(sum1, sum2);` which achieves the exact same result as writing out the full If-Then logic block.

Do custom JavaScript calculations work on mobile PDF viewers?

Custom JavaScript calculations are fully supported on standard desktop PDF editors, but many basic or default mobile PDF viewers do not support JavaScript execution. Users may need a dedicated, feature-rich PDF app to see calculations update dynamically on mobile devices.