How to Create an If-Then Calculation in a Fillable PDF Form
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.

- 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.
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.
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.
Right-click the result field where the final amount should appear and select 'Properties' from the context menu.
Go to the 'Calculate' tab within the Properties dialog box to view calculation options.
Choose the 'Custom calculation script' option and click the 'Edit' button to open the JavaScript editor.
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; }`
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.

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. Open PDF in WPS Office: Launch WPS Office and open your PDF document using the built-in PDF editor.
- 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. Set field properties: Double-click any form field to open its properties panel, where you can define formatting, data validation, and custom calculation rules.

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.




