7 Replies Latest reply on Mar 3, 2015 2:04 PM by natali.nac

    Search PDF

    natali.nac

      Hi,

      I've seen on this page how to implement the search functionality for a single word.. But what can I do if I want to search for entire senteces (or at least 2,3 words separated by space)?

        • 1. Re: Search PDF
          try67 MVP & Adobe Community Professional

          Search for the first one, and when you find it check if the following words

          after that match the rest of the words in your search phrase.

          • 2. Re: Search PDF
            gkaiseril MVP & Adobe Community Professional

            You might want to study the Acrobat JavaScript API Reference for the getPageNthWord for the syntax and optional parameters. You will need to loop through the words on a page. If you need to check the words that a on different pages your script will be more complex.

            • 3. Re: Search PDF
              natali.nac Level 1

              Thanks for the idea..

              I came up with something like this (but it doesn't work everytime)

               

              /* Find */

              var temp=[];

              var pageArray = [];

              var string = "string to search";

              var phrase= [];

              phrase= string.split(" ");

              first= phrase[0];

               

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

                  control= 0;

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

                      if (this.getPageNthWord(p, n) == first) {

                          a = n + 1;

                          control=control+1;

                          for (i = 1; i < phrase.length; i++) {

                              if (this.getPageNthWord(p, a) == phrase[i] && a< this.getPageNumWords(p)) {

                                  control=control+1;

                                  a=a+1;

                              } else break;

                          } 

                      }

                      if (control== phrase.length) {

                              pageArray.push(p);

                              break;

                      }

                  }

              }

              • 4. Re: Search PDF
                Bernd Alheit Adobe Community Professional & MVP

                /* Find */

                var temp=[];

                var pageArray = [];

                var string = "string to search";

                var phrase= [];

                phrase= string.split(" ");

                first= phrase[0];

                 

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

                    control= 0;

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

                        if (this.getPageNthWord(p, n) == first) {

                            a = n + 1;

                            control=control+1;  // <== why do you increment this variable when you find the first word on a page?

                            for (i = 1; i < phrase.length; i++) {

                                if (this.getPageNthWord(p, a) == phrase[i] && a< this.getPageNumWords(p)) {

                                    control=control+1;

                                    a=a+1;

                                } else break;

                            } 

                        }

                        if (control== phrase.length) {

                                pageArray.push(p);

                                break;

                        }

                    }

                }

                • 5. Re: Search PDF
                  natali.nac Level 1

                  The variable control gets increment when it's finds the first word of the string that should be found in the document.

                  var string = "string to search";

                  var phrase= [];

                  phrase= string.split(" ");

                  first= phrase[0]; -> so first contains the word "string"

                   

                  if (this.getPageNthWord(p, n) == first) {

                              a = n + 1;

                              control=control+1;  // <== why do you increment this variable when you find the first word on a page?

                   

                  And when on a page we find this word, it means we get a hit and we should look if the next words are "to" and "search"

                  • 6. Re: Search PDF
                    Bernd Alheit Adobe Community Professional & MVP

                    When the page contains three times the word "string" and no other word you will get the value 3 for control.

                    • 7. Re: Search PDF
                      natali.nac Level 1

                      I have now put the reset of the control variable in the inner for loop:

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

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

                                control= 0;


                      and it seems to be working.