PDF Editing Issues

How to Highlight the First Text Match and Show an Alert using Acrobat JavaScript

Natalie TaylorNatalie Taylor Nov 3, 2026 869 views

Question details

The user needs to modify an Acrobat JavaScript batch script to process only the first matching redaction annotation into a highlight, and to display an alert if no text match is found in the document.

How to Highlight the First Text Match and Show an Alert using Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Running a custom JavaScript batch process to convert redaction annotations to highlights conditionally based on search results.
Observed behavior
The current script iterates through all annotations instead of stopping after the first match, and fails to trigger the 'Text not found' alert correctly when zero matches occur.
Before you start

Always test your JavaScript on a backup copy of your PDF document to prevent accidental data loss or irreversible annotation changes.

Solution 1Recommended

Correct the Script Initialization, Loop, and Alert Logic

Fix the execution order by initializing the document object before fetching annotations, adding a break command to stop after the first match, and moving the alert evaluation outside of the loop.

The most common issue with batch scripts failing to stop or alert properly is the placement of variables and conditions. If an alert is placed inside the loop, it may trigger multiple times or evaluate incorrectly against the total length of the array. The document object must also be fully declared before any methods like 'getAnnots' are called.

1
Initialize the document object

Ensure you declare the document object before requesting the annotations. Place 'var oDoc = event.target;' or your specific document reference at the very beginning of the script, before calling 'oDoc.getAnnots()'.

2
Set up a match counter

Before starting your 'for' loop, define a counting variable such as 'var matchFound = 0;'. This will track whether any redaction annotations were successfully processed.

3
Add a break statement to the loop

Inside the loop, verify the annotation type using 'if (aAnnts[i].type == "Redact")'. Within this block, change the type to "Highlight", apply the stroke color, increment your counter ('matchFound++'), and immediately execute a 'break;' command to exit the loop so only the first match is affected.

4
Position the alert outside the loop

After the closing bracket of the 'for' loop, evaluate your counter. Add the condition 'if (matchFound === 0) { app.alert("Text not found"); }' so the alert only triggers once, and only if no matches were processed.

Correct the Script Initialization, Loop, and Alert Logic
Syntax Check: Make sure to copy and paste your exact annotation types and field names, and verify that there are no missing spaces or misplaced brackets in your conditional blocks.
Simplify Your PDF Workflow

Easily Search and Highlight Text in WPS PDF

Avoid complex JavaScript debugging by using the built-in graphical tools in WPS Office. You can easily search, highlight, and manage annotations directly from the user interface without writing a single line of code.

  1. 1. Open your document: Launch WPS Office and open your PDF file using the WPS PDF editor.
  2. 2. Access the Comment tools: Navigate to the 'Comment' tab on the top ribbon to view all available annotation options.
  3. 3. Search and Highlight: Press Ctrl+F to open the search panel, locate your first text match, and click the 'Highlight' button to apply the color instantly.
No coding or scripting required for basic annotationsOne-click text search and instant highlightingFully compatible with Microsoft Office formats and standard PDF filesLightweight, fast, and easy to navigate
microsoft office alternative - wps office

Frequently Asked Questions

Why is 'getAnnots' returning undefined in my Acrobat script?

This usually happens if the document object ('oDoc') is not properly initialized before calling the function. Always ensure 'var oDoc = event.target;' or 'this' is declared prior to fetching annotations.

How do I stop a JavaScript loop after the first match?

You can use the 'break;' statement inside your 'for' or 'while' loop block. Once your specific condition is met and the code executes, the break command immediately halts the loop and moves to the next part of the script.

Can WPS PDF run advanced Acrobat JavaScript batch scripts?

WPS PDF supports basic JavaScript for form calculations and validations. However, complex batch processing scripts utilizing Adobe-specific proprietary APIs may need to be adjusted, or the tasks can simply be performed using WPS Office's built-in graphical annotation tools.