PDF Editing Issues

How to Count Specific PDF Form Field Values Using Acrobat JavaScript

Tauseeq MagsiTauseeq Magsi Nov 3, 2026 869 views

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.

How to Count Specific PDF Form Field Values with Acrobat JavaScript
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.
Before you start

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).

Solution 1Recommended

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.

1
Access Form Preparation Mode

Open your PDF form in Adobe Acrobat or a compatible PDF editor and enter 'Prepare Form' mode from the tools menu.

2
Open Field Properties

Locate the destination field named 'TotalOutput'. Right-click on it and select 'Properties' from the context menu.

3
Enable Custom Calculation

Navigate to the 'Calculate' tab in the Properties dialog box and select the option for 'Custom calculation script'.

4
Insert the JavaScript Code

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;

5
Save and Test

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.

Apply a Custom Calculation Script to the TotalOutput Field
Syntax Note: The expression `(i < 10) ? 'Output_0' + i : 'Output_' + i` ensures that single-digit numbers are formatted with a leading zero (e.g., Output_01) to perfectly match your existing form field names.
Manage and Edit PDFs Easily

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. 1. Launch WPS PDF: Open WPS Office and click on the 'PDF' tab on the home screen to access the PDF toolset.
  2. 2. Open Your PDF Form: Click 'Open' or simply drag and drop your interactive PDF form into the workspace.
  3. 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.
Seamless compatibility with standard PDF forms and documents.Lightweight application that runs smoothly on Windows, Mac, iOS, and Android.All-in-one suite covering PDF, Word, Excel, and PowerPoint.
microsoft office alternative - wps office

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.