Calculate a PDF Form Average in Adobe Acrobat While Ignoring Blanks and Zero
Question details
Calculate the average of five PDF form fields, ignoring blanks and leaving the result blank if any field contains zero.

- Product
- Adobe Acrobat PDF form JavaScript
- Device & OS
- not provided
- Scenario
- A PDF form uses fields Q4i1 through Q4i5 as inputs and AvgQ4Score as the calculated result.
- Observed behavior
- AvgQ4Score should show the average of populated numeric fields unless any input is zero; it should also remain blank when there are no numeric inputs.
Open the PDF in an Acrobat edition that can edit form fields and confirm that the input fields are named Q4i1 through Q4i5 and the result field is named AvgQ4Score. Make sure the form will be used in a PDF viewer that supports JavaScript form calculations.
Add a custom calculation script to AvgQ4Score in Adobe Acrobat
Use this script to average populated numeric fields while returning a blank result if any input is zero.
In Adobe Acrobat, open the PDF and select All tools > Prepare a form, or Tools > Prepare Form, depending on your version.
Double-click the AvgQ4Score field to open its properties, select the Calculate tab, and choose Custom calculation script. Click Edit.
Paste the following script into the JavaScript editor, then click OK: var count = 0; var sum = 0; var hasZero = false; for (var i = 1; i <= 5; i++) { var f = this.getField("Q4i" + i); if (f.valueAsString != "" && !isNaN(Number(f.value))) { if (Number(f.value) == 0) hasZero = true; count++; sum += Number(f.value); } } if (count > 0 && !hasZero) event.value = sum / count; else event.value = "";
Close the field properties and save the PDF. Enter 2 and 4 in two input fields and leave the others blank to check that AvgQ4Score displays 3; then enter 0 in another input and check that the result becomes blank.

Work with PDFs and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Office documents. Its format compatibility makes it easy to move existing files into your workflow. For this PDF form calculation, use a PDF editor and viewer that support custom form JavaScript.

Frequently Asked Questions
What happens if all five input fields are blank?
The script sets AvgQ4Score to a blank value because there are no numeric inputs to average.
Does a zero count as a blank field?
No. A zero is detected separately: if any populated input equals zero, the script leaves AvgQ4Score blank even when other fields contain numbers.
Where do I change the number of decimal places in the result?
In Acrobat's Prepare a form tool, open the AvgQ4Score field properties and use the Format tab to choose a number format and decimal places.
Why does the calculation work in Acrobat but not in my browser?
Some browser PDF viewers do not support PDF form JavaScript. Open the saved form in a viewer that supports JavaScript calculations and test it there.




