How to Calculate an Average Using Only Completed PDF Form Fields
Question details
The user needs to calculate the average of numeric values entered into specific PDF form fields, ensuring that blank or non-numeric fields are excluded from the calculation.

- Product
- PDF Form Editor
- Device & OS
- not provided
- Scenario
- Creating or editing a fillable PDF form with auto-calculating fields.
- Observed behavior
- Needs to execute a dynamic calculation that sums only filled fields and divides by the exact count of those fields, avoiding division errors caused by empty inputs.
Ensure that your PDF fields have a consistent naming pattern (e.g., 'Field1', 'Field2') so you can easily loop through them using a JavaScript calculation script.
Use a JavaScript Loop to Calculate the Average
Implement a custom JavaScript calculation script in your target average field that iterates through the form fields, skips blanks, and computes the correct average.
By using a consistent naming pattern for your PDF form fields, you can write a simple loop to generate each field name and retrieve its value. If the value is not empty and is a valid number, the count increases, and the value is added to the sum.
Open the PDF form in your PDF editor, right-click the text field where you want the average to appear, and select 'Properties'.
Go to the 'Calculate' tab, choose the 'Custom calculation script' option, and click the 'Edit' button.
Input a loop script tailored to your field names. For example: var nSum = 0; var nCnt = 0; for (var i = 1; i <= 15; i++) { var nVal = this.getField('Qual' + i + 'm').value; if (!/^\s*$/.test(nVal) && !isNaN(nVal)) { nCnt++; nSum += nVal; } } event.value = (nCnt !== 0) ? (nSum / nCnt) : '';
Click 'OK' to save the script, close the properties window, and test your form by entering numbers into the corresponding fields.

Create and Calculate PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to create fillable forms, set field properties, and implement custom calculations effortlessly.
- 1. Open PDF in WPS: Launch WPS Office and open your PDF document in the PDF editor.
- 2. Access Form Tools: Navigate to the 'Form' tab to insert text fields, checkboxes, or dropdowns.
- 3. Set Calculation Scripts: Double-click the destination field, navigate to the calculation settings, and input your averaging formula.

Frequently Asked Questions
Why is the default average calculation including empty fields as zeros?
Standard built-in average functions in some PDF editors may treat empty fields as zero, which skews the result. Using a custom JavaScript loop allows you to explicitly ignore empty strings before counting them.
What happens if a user inputs text instead of numbers in the fields?
The provided JavaScript uses the isNaN() function to verify if the input is a valid number. If text is entered, it will be ignored and excluded from both the sum and the total count.
How do I name my fields so the JavaScript loop works?
Name your fields with a consistent prefix and an incrementing number suffix, such as 'Field1', 'Field2', 'Field3'. The loop script uses this structure to dynamically fetch each field using this.getField('Field' + i).




