How to Count Specific Text Values in Acrobat PDF Form Fields
Question details
The user needs to automatically count how many times specific text values (like 'C' or 'D') appear across multiple form fields in a PDF, while ignoring any empty fields.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms that require calculating the total occurrences of specific text inputs across numerous sequentially named fields.
- Observed behavior
- The user needs to set up calculation fields using custom JavaScript to tally specific string values accurately across up to 135 fields.
Ensure you have the exact names for all your form fields noted down, as the custom script relies on sequential naming patterns (e.g., cd.1.1, cd.1.2) to loop through and count the values correctly.
Use a Custom Calculation Script to Loop Through Fields
Use JavaScript within a calculation field's properties to iterate through sequentially named form fields and count occurrences of specific text.
This method requires your target fields to be named in a predictable, numbered sequence so the script can loop through them using a counter variable.
Open your PDF form in Acrobat, navigate to the Tools menu, and select Prepare Form.
Right-click the specific field where you want the total count for 'C' to appear and select Properties.
Navigate to the Calculate tab, select the 'Custom calculation script' option, and click the Edit button.
Enter the following script, adjusting the prefix 'cd.1.' and the maximum number '135' to match your actual form fields: var total = 0; for (var i = 1; i <= 135; i++) { if (this.getField("cd.1." + i).valueAsString === "C") total++; } event.value = total;
For the total 'D' field, repeat this entire process on a new calculation field, changing the script comparison from "C" to "D".

Use Array Filtering as an Alternative Script
Extract all field values into an array and filter it to find the total count of specific matching values.
Easily Edit and Manage PDF Forms with WPS Office
WPS PDF provides comprehensive tools to create, edit, and fill out interactive PDF forms. You can effortlessly manage form fields, customize properties, and handle complex daily PDF tasks in a highly compatible environment.
- 1. Open Your PDF: Launch WPS Office and open your PDF document containing the forms you need to edit.
- 2. Navigate to the Form Tab: Go to the 'Form' tab on the top ribbon to access all form creation and editing tools.
- 3. Add Form Fields: Click 'Text Field', 'Check Box', or other form controls to add new interactive fields to your document.
- 4. Edit Field Properties: Double-click any added field to access its Properties, where you can customize its appearance, behavior, and formatting.

Frequently Asked Questions
Why is my calculation script not updating the total?
Ensure that your form field names exactly match the naming convention used in your JavaScript loop. Also, check the calculation order in your PDF properties to ensure the total field calculates after the individual input fields are filled.
Does case sensitivity matter in the script?
Yes, JavaScript is case-sensitive. The strict comparison in the script (=== "C") will only count uppercase letters. If you want to count both cases, you must adjust your script to account for lowercase letters as well.
How do I handle empty fields in the calculation?
The provided script specifically checks if the field value exactly matches your target letter. Any empty fields or fields containing other text are automatically ignored during the loop, requiring no extra steps.




