7 Replies Latest reply on Nov 14, 2012 12:52 PM by BS_Singularity

    Automatic Creation of Bookmarks in Javascript

    BS_Singularity

      I need to create a bookmark based on a random string of text located in the same place on every page and I have approx. 1,000 pages to work with.

       

      For example, on every page there will be a string that has a format of XX-XXX-XX-XXXXX-XX.  It will be in the same location on every page.  The bookmark will need that string's value and point to the page that particular string occurs on.

       

      So on page 2 the string is Z1-0AA-D3-P99N2-AP, the bookmark inherits that name, and points to page 2.

       

      I have been searching these forums and the internet in general.  I realise I can use the following to start off with but I don't know how to find the string, name the bookmark after it, and then what is the correct value for X?  I don't think it would be this.pageNum, but I could be wrong.

       

      var FoundString = /\d{2}+"-"+\d{3}+"-"+\d{2}+"-"+\d{5}+"-"+\d{2}/ ?

       

      Edit: var FoundString = /\d{2}\W\d{3}\W\d{2}\W\d{5}\W\d{2}/

       

      this.bookmarkRoot.createChild({FoundString, X})

       

       

      I don't use javascript often so I'd definitely appreciate any assistance you can offer.

        • 1. Re: Automatic Creation of Bookmarks in Javascript
          gkaiseril MVP & Adobe Community Professional

          You might want to start with a much smaller starting point, like one page with just the data you want to search for and see how your script works.

           

          I would start with the 'getPageNthWord' method and see how it works and the various parameters for that method work.

           

          Your RegExp is not valid to use for a the 'RE.test' method.

           

          Since you are using the 'getPageNthWord' you will now the page number for your script to set the bookmark action for going to a given page.

          1 person found this helpful
          • 2. Re: Automatic Creation of Bookmarks in Javascript
            BS_Singularity Level 1

            Thank you for your reply!

             

            I am starting small with the search function.  I'm trying to get it to find the string and then spit it back to me from one page.

             

            I will definitely look into getpagenthword.  Can you tell me why my RegExp won't work?  I believe you, I just want to know why it won't work.

            • 3. Re: Automatic Creation of Bookmarks in Javascript
              BS_Singularity Level 1

              Alright, I've been playing around with getPageNthWord and have successfully ID'd the correct line on the page.

               

              {

              var str1, str2, str3, str4, str5, FoundString;

              str1 = this.getPageNthWord(0,218)

              str2 = this.getPageNthWord(0,219)

              str3 = this.getPageNthWord(0,220)

              str4 = this.getPageNthWord(0,221)

              str5 = this.getPageNthWord(0,222)

              FoundString = str1 + "-" + str2 + "-" + str3 + "-" + str4 + "-" + str5;

              }

               

              I don't know if I'm going to continue with the bookmarking part, for I may just create a simple spreadsheet listing the string, which page it is on, in which PDF.

               

              Any suggestions on what I already have?

              • 4. Re: Automatic Creation of Bookmarks in Javascript
                gkaiseril MVP & Adobe Community Professional

                The problem you are facing is that Acrobat JavaScript uses white space characters to split for words. A "-" is a white space character and causes a number formatted number like "12-123-12-12345-12" to be split into 5 words and using the default for white space does not show the "-".

                 

                You need to read the words on the page one at a time but look at 5 at time and when you use the "getPageNthWord" you need to set the "bStrip" parameter to false.

                 

                So to test the ith word on page 1

                 

                var cTestSring;

                var nPageNum = 0;

                 

                for(i = 0; i < this.getPageNumWords({nPage: nPageNum}) - 5; i++) {

                cTestString = this.getPageNthWord({nPage: nPageNum, nWord: i, bStrip = false});

                cTestString += this.getPageNthWord({nPage: nPageNum, nWord: i + 1, bStrip = false});

                cTestString += this.getPageNthWord({nPage: nPageNum, nWord: i + 2, bStrip = false});

                cTestString += this.getPageNthWord({nPage: nPageNum, nWord: i + 3, bStrip = false});

                cTestString += this.getPageNthWord({nPage: nPageNum, nWord: i + 4, bStrip = false});

                cTestString += this.getPageNthWord({nPage: nPageNum, nWord: i + 5, bStrip = false});

                if(/^\d{2}[-]\d{3}[-]\d{2}[-]\d{5}[-]\d{2}[ ]$/.test(cTestString) == true) {

                // set bookmark for this pge

                app.alert("found account: " + cTestString);

                }

                 

                Or something like that.I have not tested the code.

                 

                I have bumped into this problem when trying to find multiple words. Also PDF words do not always by number do not always appear in the numeric sequence so it is possible for an account to be missed.

                1 person found this helpful
                • 5. Re: Automatic Creation of Bookmarks in Javascript
                  BS_Singularity Level 1

                  I've found the internal structure of the PDF is wildly different from what you see.  The string I'm looking for should be the fourth text on the page but as you can see from the example I posted above it is in the hundreds.

                   

                  I'm definitely exploring your latest option and will notify you what I find!

                  • 6. Re: Automatic Creation of Bookmarks in Javascript
                    BS_Singularity Level 1

                    I figured out bStrip = false needs to be bStrip: false or the whole thing collapses.

                     

                    However cTestString is failing to be set to the necessary string.  Instead it returns a different line of information.  I don't know why yet, but I'm still fiddling with it.  Learning as I go!

                    • 7. Re: Automatic Creation of Bookmarks in Javascript
                      BS_Singularity Level 1

                      Okay, after some consulting with a fellow co-worker, here are two scripts that work (both are his scripts):

                       

                      Absolute - this is for when you're 100% positive the position of the words never move.

                       

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

                          var v1 = this.getPageNthWord(i,218);

                          var v2 = this.getPageNthWord(i,219);

                          var v3 = this.getPageNthWord(i,220);

                          var v4 = this.getPageNthWord(i,221);

                          var v5 = this.getPageNthWord(i,222);

                          var vis = v1 + "-" + v2 + "-" + v3 + "-" + v4 + "-" + v5;

                          this.bookmarkRoot.createChild(vis,"this.pageNum ="+ i,i);

                      }

                       

                       

                      Regex - this will loop through and make sure the Regular Expression matches before creating the bookmark.  Best used if the word position moves.  It is really slow, though.  Really really slow.

                       

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

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

                              var v1 = this.getPageNthWord(i,j);

                              var v2 = this.getPageNthWord(i,j+1);

                              var v3 = this.getPageNthWord(i,j+2);

                              var v4 = this.getPageNthWord(i,j+3);

                              var v5 = this.getPageNthWord(i,j+4);

                              var vis = v1 + "-" + v2 + "-" + v3 + "-" + v4 + "-" v5;

                              var reg = /\d{2}-\d{3}-\d{2}-\d{5}-\d{2}/;

                              if(reg.test(vis)){

                                  break;

                              }

                          }

                          this.bookmarkRoot.createChild(vis,"this.pageNum ="+ i,i);

                      }

                       

                      Hopefully this helps someone out in the future!