PDF Editing Issues

Use Acrobat Action Wizard JavaScript to Find Text and Add PDF Watermarks

Kushani NimanthikaKushani Nimanthika Nov 18, 2026 868 views

Question details

The user needs to batch watermark PDFs when specified phrases appear in the required order.

Use Acrobat Action Wizard JavaScript to Find Text and Add PDF Watermarks
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.
Before you start

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.

Solution 1Recommended

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.

1
Open a test PDF

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.

2
Test the script

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"}); }

3
Check the result

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.

4
Create the action

Open Tools > Action Wizard and choose New Action. Add Execute JavaScript from the available tools, open Specify Settings, and paste the tested script.

5
Run and save the batch

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.

Test an ordered text search before adding it to Action Wizard
Check existing watermarks: A new Acrobat watermark can replace an existing watermark on affected pages. To watermark only matching pages, use page-specific search logic and the watermark method's page-range options.
Free Microsoft Office alternative

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.

Work with PDFs and access watermark tools for routine document preparationOpen and edit Microsoft Word, Excel, and PowerPoint formats, including DOCX, XLSX, and PPTXUse a familiar interface that makes moving existing Office files straightforwardGet a lightweight office suite with a free version
microsoft office alternative - wps office

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.