PDF Editing Issues

Count Phrase Occurrences in PDF Form Fields with Acrobat JavaScript

Algirdas JasaitisAlgirdas Jasaitis Nov 19, 2026 868 views

Question details

Count how many times a phrase entered in one PDF form field appears in article text stored in another field.

Count Phrase Occurrences in PDF Form Fields with Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
A PDF form contains an article text field, a search phrase field, and a field for displaying the count.
Observed behavior
The count field should display the number of occurrences of the entered phrase in the article field.
Before you start

Open the PDF in Adobe Acrobat and confirm that the article text and search phrase are stored in separate form fields. Note their exact field names and create a third text field for the result if needed.

Solution 1Recommended

Add a JavaScript calculation to the PDF count field

Use a custom calculation to count literal, case-sensitive occurrences in the article form field.

This method searches text stored in a form field, not the visible text throughout the PDF. It counts non-overlapping matches and treats punctuation in the phrase as literal text.

1
Open the form editor

In Acrobat, open the PDF and select Prepare a form from the tools or menu. Identify the article, phrase, and result text fields.

2
Open the calculation script

Open Properties for the result field, select the Calculate tab, choose Custom calculation script, and click Edit.

3
Enter the script

Paste the following script, replacing ArticleText and SearchPhrase with the exact names of your fields: var article = this.getField("ArticleText").valueAsString; var phrase = this.getField("SearchPhrase").valueAsString; if (phrase === "") { event.value = 0; } else { var escaped = phrase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); var matches = article.match(new RegExp(escaped, "g")); event.value = matches ? matches.length : 0; }

4
Save and test the form

Confirm the script, close form editing, and save the PDF. Enter article text and a phrase in their fields, then move focus out of the edited field and check the result.

Add a JavaScript calculation to the PDF count field
Searching page text: The script does not extract ordinary PDF page text. Searching a whole document would require collecting its page text first; Acrobat may expose that text one word at a time, which can be slow for large files.
Free Microsoft Office alternative

Work with PDF and Office files in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with PDF and Office files. Its format compatibility makes it easier to move existing documents into your workflow, though it does not replace the Acrobat-specific JavaScript calculation above.

Open and review PDF documents alongside your other files.Work with Microsoft Office formats, including DOCX, XLSX, and PPTX.Use a familiar interface to ease migration from Microsoft Office.Access document tools in a lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Does the script count matches in the whole PDF?

No. It searches only the text in the ArticleText form field. To search ordinary page text, you would first need to extract and assemble that text.

Is the phrase count case-sensitive?

Yes. The script uses the global regular-expression flag "g". Change it to "gi" in new RegExp(escaped, "g") to count matches without regard to case.

What happens if the search phrase contains punctuation?

The script escapes regular-expression special characters before searching, so characters such as periods, parentheses, and plus signs are matched literally.

Why does the result stay at zero when the phrase field is empty?

The calculation explicitly sets event.value to 0 for an empty phrase, avoiding meaningless empty-string matches.

What should I check if the count does not update?

Check that ArticleText and SearchPhrase exactly match the form field names, that the script is in the result field's Calculate tab, and that field calculations are enabled in Acrobat's form preferences.