How to Count Nonempty Fields in a PDF Form Array using JavaScript
Question details
The user needs to count the number of fields containing text in a PDF form array (named Pax1 through Pax40) and display the total count in a specific summary field.

- Product
- Adobe Acrobat PDF
- Device & OS
- not provided
- Scenario
- Designing an interactive PDF form that requires automatic tallying of populated data fields.
- Observed behavior
- Requires a custom JavaScript calculation to iterate through dynamically named fields, verify if they contain text, and output the final count.
Ensure you have a PDF editor that supports form field creation and custom JavaScript calculations, and verify the exact names of your target form fields.
Use a Custom JavaScript Calculation Script
Implement a 'for' loop in the destination field's calculation script to check each target field's value and tally the non-empty ones.
By utilizing the valueAsString property in Adobe Acrobat JavaScript, you can accurately verify if a form field contains text. A simple loop will iterate through the sequenced field names (e.g., Pax1 to Pax40) and increment a counter for each field that is not blank.
Open your PDF form in edit mode. Right-click the destination field (the field where you want the final count, e.g., 'PAX') and select 'Properties'.
In the Properties dialog box, click on the 'Calculate' tab. Select the 'Custom calculation script' radio button.
Click the 'Edit' button and input your loop script. For example: var count = 0; for (var i = 1; i <= 40; i++) { var f = this.getField('Pax' + i); if (f !== null && f.valueAsString !== '') { count++; } } event.value = count;
Click 'OK' to save the script, then close the Properties dialog. Exit form editing mode and type text into a few 'Pax' fields to verify that the 'PAX' total field updates automatically.
Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to easily create, edit, and manage interactive form fields, offering a seamless alternative for your document data collection needs.
- 1. Open the PDF: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon menu to access interactive field tools.
- 3. Add Form Fields: Select 'Text Field' or other controls to seamlessly build out your data collection arrays.
- 4. Save and Distribute: Save your newly created interactive PDF form and share it with your users.

Frequently Asked Questions
Why is my PDF JavaScript calculation not updating automatically?
Make sure that the calculation order is correct in your PDF form settings. Additionally, check your application preferences to ensure that automatic field calculation is enabled.
Can I use this script for checkboxes instead of text fields?
Yes, but you will need to modify the condition. Instead of checking if valueAsString is an empty string, you should check if the field's value is not equal to 'Off' (or whatever export value is assigned to the unchecked state).
What happens if one of the fields in the array doesn't exist?
If a field is missing, the getField() method returns null. It is best practice to include a null check (e.g., if (f !== null)) in your loop before accessing properties to prevent script errors from breaking the calculation.




