PDF Editing Issues

Calculate the Average of Two PDF Form Fields with JavaScript

Chanuka GeekiyanageChanuka Geekiyanage Nov 5, 2026 869 views

Question details

The user needs to calculate the average of two PDF form fields (e.g., forward and aft draft) and subtract that average from a third field using a custom JavaScript formula.

How to Calculate the Average of PDF Form Fields Using JavaScript
Product
PDF
Device & OS
not provided
Scenario
Creating an advanced interactive PDF form that requires custom mathematical operations and multiple referenced field values.
Observed behavior
The current formula returns an incorrect calculation because the division occurs before the sum of the two fields is calculated, requiring corrected parentheses placement in the JavaScript.
Before you start

Ensure that the field names in your PDF exactly match the names you intend to use in your JavaScript code, as JavaScript is case-sensitive and will not recognize mismatched names.

Solution 1Recommended

Apply Correct Order of Operations in Custom JavaScript

Fix the mathematical formula by wrapping the sum of the two fields in parentheses to calculate the average first before subtracting it from the primary field.

In JavaScript, standard mathematical order of operations (PEMDAS) applies. If you do not isolate the addition operation in parentheses, only the second field will be divided by two before being added to the first field.

1
Open Field Properties

Enter form editing mode in your PDF editor, right-click the text field where you want the final calculated result to appear, and select Properties.

2
Navigate to the Calculate Tab

In the Properties dialog box, click on the Calculate tab and select the 'Custom calculation script' option.

3
Enter the Corrected Formula

Click the Edit button and paste the following formula: event.value = this.getField("Keel to Truck").value - ((this.getField("FWD DRAFT").value + this.getField("AFT DRAFT").value) / 2); Replace the field names with your specific field labels.

4
Save and Test

Click OK to close the script editor, then close the Properties window. Enter test numbers into your draft fields to verify that the subtraction and average calculation work correctly.

Apply Correct Order of Operations in Custom JavaScript
Parentheses Placement: Placing parentheses correctly around (FWD DRAFT + AFT DRAFT) ensures their sum is determined first, giving you an accurate average before the subtraction step.
Advanced PDF Form Editing

Easily Create and Calculate PDF Forms in WPS Office

WPS PDF provides comprehensive tools for creating interactive forms, including full support for custom JavaScript calculations, conditional formatting, and field validations.

  1. 1. Open Your PDF: Launch WPS Office and open your PDF document containing the interactive form fields.
  2. 2. Enter Form Mode: Navigate to the 'Form' tab on the top ribbon to view, edit, or create form fields.
  3. 3. Set Custom Calculations: Double-click your target text field to open its properties, switch to the Calculate tab, and insert your custom JavaScript formula seamlessly.
Easily insert text fields, checkboxes, and drop-down listsFull support for Adobe Acrobat JavaScript API for complex calculationsHigh compatibility with standard PDF form formats across platformsFree and lightweight alternative to heavy PDF editors
QA img-9

Frequently Asked Questions

Why is my JavaScript calculation showing an incorrect total in my PDF?

This usually happens due to missing parentheses in your mathematical formula. According to the standard order of operations, division occurs before addition. Always wrap the fields you want to average in an extra set of parentheses before dividing by the total count.

Can I use built-in functions to calculate an average in a PDF form?

Yes, PDF form properties have a built-in 'Value is the average of the following fields' option under the Calculate tab. However, if you need to perform additional math with that average (such as subtracting it from another total), you must bypass the built-in option and use a custom JavaScript calculation instead.

What happens if a referenced form field is left blank?

By default, an empty text field in a PDF evaluates to zero in JavaScript calculations. To prevent this from skewing your average or displaying an error, you can add conditional JavaScript (e.g., if statements checking for empty values) to ensure the calculation only runs when the required fields are filled.