Skip to main content
Inspiring
February 20, 2019
Answered

Deleting Pages Containing A Specific String Using this.getPageNumWords()

  • February 20, 2019
  • 2 replies
  • 1968 views

I have been trying to delete a particular page using the following script provided by Acrobat JS aficionado Karl Heinz Kremer. This code is in the mouse up event of a button. The text "HW2019-02" is searchable on the page (not an image and not a field value).Unfortunately, I just cannot get it to work. Does anyone have a fix for this?

for (var p=this.numPages-1; p>=0; p--) {

    for (var n=0; n<this.getPageNumWords(p); n++) {

        if (this.getPageNthWord(p, n) == "HW2019-02") {

            this.deletePages(p);

            break;

        }

    }

}

Thank you in advance

This topic has been closed for replies.
Correct answer gkaiseril

Have you read the Acrobat JavaScript Reference about the getPageNthWord?

Does this method return the string you are looking for or something else?

There is mention of punctuation and other white space. White space contains special characters like the "-" so you may need to provide for this type of character in your script.

Have you modified your script to display the words that are being found by your script?

You may need to include the "bStrip" parameter to pickup the non-alphanumeric character.

2 replies

Thom Parker
Community Expert
Community Expert
February 20, 2019

You just can't say it doesn't work, you have to tell us what debug you've done and what is happening.

What is the exact result that you get?  Are any pages deleted? No pages?

Are any errors reported in the console window?

Do you know if the searched word is ever found?

Did you print out the results of a search on a page where you know the word exists? If you had, you'd probably see that Acrobat splits it into two words at the hyphen.  To find this word you need to modify the loop to collect two or more words at a time. Then search the collected string.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
February 20, 2019

-NOTHING-

Absolutely nothing happened.. I shared the code and explained what the result was.

//====================

for (var p=this.numPages-1; p>=0; p--) { 

    if (this.numPages==1) break; 

    for (var n=0; n<this.getPageNumWords(p)-1; n++) { 

        if (this.getPageNthWord(p, n) == "HW2019-02") { 

            this.getField("TestIt").value = p; <----I tried to capture 'p' but nothing

            this.deletePages(p); 

            break; 

        } 

    } 

try67
Community Expert
Community Expert
February 20, 2019

As Thom suggested, it's not a single word, but two.

Bernd Alheit
Community Expert
Community Expert
February 20, 2019

What happens when you search for "HW2019" ?