How to Set PDF Form Field to Calculate the Greater of Two Fields
Question details
The user needs to dynamically calculate and display the higher numerical value between two specified text fields into a third field within a fillable PDF form.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that requires comparative numerical calculations to output a maximum value.
- Observed behavior
- The user wants a destination field to automatically evaluate two separate numerical input fields and output the highest number using a custom calculation.
Ensure you are editing the PDF in 'Prepare Form' mode and verify the exact, case-sensitive names of the input fields you intend to compare.
Use Custom Calculation JavaScript
Apply a simple JavaScript utilizing the Math.max() function to dynamically evaluate and retrieve the highest value between two form fields.
PDF forms support custom JavaScript for complex calculations that go beyond simple addition or multiplication. By utilizing the built-in Math.max() function, you can automatically capture the values of two input fields, convert them to numbers, and output the larger value into your target field.
Open your PDF form, enter form editing mode, right-click the destination field (the one you want the result to appear in), and select 'Properties'.
At the top of the Text Field Properties window, click on the 'Calculate' tab.
Choose the option labeled 'Custom calculation script' and click the 'Edit' button next to it.
Enter the following code in the JavaScript editor: var a = Number(this.getField('a').value); var b = Number(this.getField('b').value); event.value = Math.max(a, b);
Click 'OK' to save your script, close the properties window, and test your form by entering values into field 'a' and field 'b'.

Create and Manage PDF Forms Seamlessly with WPS Office
WPS Office provides a comprehensive PDF editor equipped with robust form-building capabilities. Whether you are adding interactive text fields, calculating values, or simply filling out applications, WPS PDF handles complex tasks efficiently.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document. Navigate to the 'Form' tab on the top ribbon.
- 2. Edit Form Properties: Double-click the specific text field where you want the calculation to appear to open its Properties dialog.
- 3. Apply Calculations: Switch to the 'Calculate' section, choose 'Custom calculation script', enter your JavaScript, and close the dialog to apply the changes.

Frequently Asked Questions
Why is my custom PDF calculation script not updating the field?
If the field is not updating, double-check that the field names inside your JavaScript (e.g., this.getField('FieldName')) precisely match the names of the fields on your form. Additionally, ensure that your PDF viewer has JavaScript execution enabled.
Can I calculate the minimum value instead of the maximum?
Yes. You can achieve this by simply replacing Math.max(a, b) with Math.min(a, b) in your custom calculation script. This will output the lesser of the two values.
How do I calculate the greater of three or more fields?
You can extend the JavaScript Math.max function to evaluate multiple variables. For instance, define a third variable 'c' and adjust the final line of your script to read event.value = Math.max(a, b, c);
Will this JavaScript calculation work when filling the form on mobile devices?
It depends on the PDF viewer app. While advanced desktop editors like Adobe Acrobat and WPS Office fully support JavaScript calculations, many basic or default mobile PDF readers restrict or ignore custom JavaScript for security and performance reasons.




