PDF Editing Issues

How to Set PDF Form Field to Calculate the Greater of Two Fields

Khadija KhanKhadija Khan Nov 24, 2026 868 views

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.

How to Set a PDF Form Field to Calculate the Greater of Two Values
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Field Properties

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'.

2
Navigate to the Calculate Tab

At the top of the Text Field Properties window, click on the 'Calculate' tab.

3
Select Custom Script Option

Choose the option labeled 'Custom calculation script' and click the 'Edit' button next to it.

4
Input the JavaScript Code

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);

5
Save and Test

Click 'OK' to save your script, close the properties window, and test your form by entering values into field 'a' and field 'b'.

Use Custom Calculation JavaScript
Match Your Field Names Exactly: Replace 'a' and 'b' in the script with the actual names of the fields in your PDF document. JavaScript is case-sensitive, so capitalization matters.
Advanced PDF Editing Made Easy

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. 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document. Navigate to the 'Form' tab on the top ribbon.
  2. 2. Edit Form Properties: Double-click the specific text field where you want the calculation to appear to open its Properties dialog.
  3. 3. Apply Calculations: Switch to the 'Calculate' section, choose 'Custom calculation script', enter your JavaScript, and close the dialog to apply the changes.
Fully compatible with standard Adobe Acrobat PDF forms and formatsIntuitive interface for adding text fields, checkboxes, and interactive elementsSeamless transition and familiar UI for users migrating from other office suitesLightweight application that loads and calculates PDF forms instantly
QA img-9

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.