How to Count Specific PDF Form Field Values Using Acrobat JavaScript
Question details
The user needs to count how many times the exact word 'Correct' appears across multiple sequentially named PDF form fields (Output_01 to Output_20) and display the resulting count divided by two in a final TotalOutput field.

- Product
- Adobe Acrobat / Advanced PDF Editors
- Device & OS
- not provided
- Scenario
- Creating an automated grading or assessment PDF form where specific text responses need to be aggregated, mathematically processed, and displayed as a final score automatically.
- Observed behavior
- The TotalOutput field requires a custom calculation script to dynamically loop through 20 distinct form fields, evaluate their string values, and output the scaled mathematical result.
Ensure you have access to a PDF editor that supports custom JavaScript calculations for form fields, and verify that your target form fields are named consistently (e.g., Output_01, Output_02).
Apply a Custom Calculation Script to the TotalOutput Field
Use a custom JavaScript loop within the PDF form field properties to dynamically construct field names, count the specific values, and apply the required mathematical division.
This method utilizes a JavaScript 'for' loop to iterate through the sequential field names. It checks the string value of each field and increments a counter if the value exactly matches 'Correct'. Finally, it divides the total count by 2 and assigns it to the current field's event value.
Open your PDF form in Adobe Acrobat or a compatible PDF editor and enter 'Prepare Form' mode from the tools menu.
Locate the destination field named 'TotalOutput'. Right-click on it and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab in the Properties dialog box and select the option for 'Custom calculation script'.
Click the 'Edit' button and paste the following JavaScript code: var counter = 0; for (var i = 1; i <= 20; i++) { var fname = (i < 10) ? 'Output_0' + i : 'Output_' + i; var f = this.getField(fname); if (f && f.valueAsString == 'Correct') counter++; } event.value = counter / 2;
Click 'OK' to save the script, then close the Properties dialog. Exit 'Prepare Form' mode and test the form by entering the word 'Correct' into some of the Output fields to verify that TotalOutput updates correctly.

Use WPS PDF to View and Manage Your PDF Forms
WPS Office provides a comprehensive PDF editor that allows you to view, fill, and modify PDF forms seamlessly. It is a lightweight and powerful suite for handling your daily document needs without complex configurations.
- 1. Launch WPS PDF: Open WPS Office and click on the 'PDF' tab on the home screen to access the PDF toolset.
- 2. Open Your PDF Form: Click 'Open' or simply drag and drop your interactive PDF form into the workspace.
- 3. Fill and Manage: Interact with the form fields directly. You can quickly fill out text fields, check boxes, and save your completed forms instantly.

Frequently Asked Questions
Why is my PDF JavaScript calculation not updating automatically?
If fields aren't updating, you may need to check the field calculation order in your PDF editor's form tools. Additionally, ensure that the option to automatically calculate field values is enabled in your software's preferences.
Can I count checkboxes or radio buttons instead of text fields?
Yes. You can modify the script to check the value of a checkbox or radio button. Rather than checking for a string like 'Correct', you would check if the field's value matches its designated export value (e.g., 'Yes', 'On', or 'Choice1').
What happens if a form field is empty, missing, or mistyped in the document?
The script includes a safety check `if (f && f.valueAsString == 'Correct')`. The `f &&` portion ensures that if a field does not exist, the script will simply ignore it and move to the next iteration without breaking or throwing an error.
Is Acrobat JavaScript exactly the same as web JavaScript?
While they share the exact same core language syntax (loops, variables, operators), Acrobat JavaScript includes specific objects, properties, and methods—such as `this.getField()` and `event.value`—that are unique to interacting with PDF documents.




