Skip to main content
Participating Frequently
September 14, 2018
Answered

Searching a document with search.query

  • September 14, 2018
  • 1 reply
  • 1650 views

I wrote a working script to compare every word in a document to a list of words I want to flag. It works but is incredibly slow. I know that Acrobat has an indexing feature. Maybe I can tap into that?

I've been playing with search.query, but it doesn't look like it actually returns anything. Please don't tell me to look in the manual :-)... it isn't described there much.

My loop is:

for (var i = 0; i < currentDoc.numPages; i++ )

{

...

        for (var j = 0; j < currentDoc.getPageNumWords(i); j++)

        {

         ....

              for (var n = 0; n < wordItem.length; n++)   // my list of terms I want to look for

              {

                 If found, comment-highlight the word.

               }

          }

}

Thanks for any help you can give me!

Rick

This topic has been closed for replies.
Correct answer Joel Geraci

The method search.query is a way to programmatically initiate a search. The results are available in the search panel, not to JavaScript. Unfortunately, the only way to locate words and highlight them is the way you are doing it.

That said, you can make your code FAR more efficient by not trying to add a highlight annotation while you are looping through the words. That really slows down Acrobat.

Instead, create an array of arrays for the word indices that match the terms you want to highlight. Then after all words have been identified, loop through that new array and add the highlights. Your array might look like this...

var wordsToHighlight = [

     [4,15,21],

     [],

     [19,22,25,85]

]

...so the first page (index 0 of the array) would have words 4,15, and 21 highlighted. The second page gets no highlights and the third page... well, you get the idea.

1 reply

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
September 14, 2018

The method search.query is a way to programmatically initiate a search. The results are available in the search panel, not to JavaScript. Unfortunately, the only way to locate words and highlight them is the way you are doing it.

That said, you can make your code FAR more efficient by not trying to add a highlight annotation while you are looping through the words. That really slows down Acrobat.

Instead, create an array of arrays for the word indices that match the terms you want to highlight. Then after all words have been identified, loop through that new array and add the highlights. Your array might look like this...

var wordsToHighlight = [

     [4,15,21],

     [],

     [19,22,25,85]

]

...so the first page (index 0 of the array) would have words 4,15, and 21 highlighted. The second page gets no highlights and the third page... well, you get the idea.

Participating Frequently
September 14, 2018

Great, advice... thanks! I'll give that a shot.

Rick

try67
Community Expert
Community Expert
September 14, 2018

You'll need to use the getPageNthWord method to actually find the matches to your search string, though, and if it's more than one word it becomes even more complicated...