Use Acrobat Action Wizard JavaScript to Find Text and Add PDF Watermarks
Question details
The user needs to batch watermark PDFs when specified phrases appear in the required order.

- Product
- Adobe Acrobat Pro Action Wizard
- Device & OS
- not provided
- Scenario
- Run JavaScript through an Acrobat action to examine multiple PDFs and apply a watermark or stamp to matching documents.
- Observed behavior
- Each document should be checked for the specified phrases in order before the appropriate watermark or stamp is applied.
Confirm that Acrobat Pro has Action Wizard and that the PDFs contain selectable text; run OCR first for scanned documents. Decide the exact phrases, case sensitivity, search order, and whether the watermark should appear on all pages or only matching pages.
Test an ordered text search before adding it to Action Wizard
Use this approach to apply a text watermark to every page of a PDF when two phrases occur in the specified order.
The example searches extracted text across the whole document, ignores case, and adds one watermark when the second phrase follows the first. It does not identify matching pages or apply different watermarks for different phrase combinations.
PDF text extraction may not follow the visual reading order, particularly in columns or tables. Check the extracted text and results on representative documents before processing a batch.
Open a representative PDF in Acrobat Pro and select some text to confirm that the phrases are searchable. If the document is scanned, use Scan & OCR to recognize its text first.
Open Acrobat's JavaScript console, replace the two phrases and watermark text in the script below, and run it against the open document: var doc = this; var phrase1 = "first phrase".toLowerCase(); var phrase2 = "second phrase".toLowerCase(); var text = ""; for (var p = 0; p < doc.numPages; p++) { var count = doc.getPageNumWords(p); for (var w = 0; w < count; w++) { text += doc.getPageNthWord(p, w, false) + " "; } } text = text.replace(/\s+/g, " ").toLowerCase(); var first = text.indexOf(phrase1); var second = first < 0 ? -1 : text.indexOf(phrase2, first + phrase1.length); if (first >= 0 && second >= 0) { doc.addWatermarkFromText({cText: "CONFIDENTIAL"}); }
Inspect the PDF after running the script. Test documents with both phrases in order, in reverse order, and with one phrase missing; adjust the phrase text or search logic if extraction or punctuation affects a match.
Open Tools > Action Wizard and choose New Action. Add Execute JavaScript from the available tools, open Specify Settings, and paste the tested script.
Add a Save step to the action and configure the output location and naming options. Save the action, add the PDFs to its file list, and start it; then review matching and nonmatching output files.

Edit PDFs and Office files with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Office files. It can help with everyday PDF editing and watermark tasks, although it does not replace Acrobat Action Wizard's conditional JavaScript batch workflow.

Frequently Asked Questions
Why does the script miss text I can see in the PDF?
The page may be an image, or Acrobat may extract the words in a different order or with different punctuation. Try Scan & OCR for scanned pages, then inspect the extracted text and compare it with the phrases in the script.
Does the example check phrase order across pages?
Yes. It joins extracted words from all pages in page order, so the second phrase can appear on a later page. It uses extracted reading order, which may differ from the visual layout.
How can I make the search case-sensitive?
Remove the toLowerCase() calls from both phrase definitions and from the extracted text. Enter each phrase with the exact capitalization expected in the PDFs.
Can I apply different watermarks for different phrases?
Yes. Add separate conditions to the JavaScript and call addWatermarkFromText with the appropriate text for each match. Test the conditions in the JavaScript console before adding the script to Action Wizard.
What information helps troubleshoot an Acrobat action?
Share the current script, a representative PDF if permitted, the phrase lengths, and the desired watermark behavior. File sizes and whether the phrases must appear on the same page are also useful.




