6 Replies Latest reply on Apr 13, 2015 7:59 AM by gkaiseril

    Jump to a specific bookmark using JavaScript

    lindaeliseruble

      Hello,

       

      I am able to successfully jump to a specific bookmark in a PDF document when I run the following code in the Debugging Console:

       

      // Jump to "Monthly Summary"

      for (var i=0; i<this.bookmarkRoot.children.length; i++) {

                  if (this.bookmarkRoot.children[i].name.indexOf("Monthly Summary")!=-1) {

                     this.bookmarkRoot.children[i].execute();

                  }

      }

       

       

      The problem is that I would like to be able to run this code in a folder level JavaScript file ,however, the above code does not work for me.

       

      What must I change to make this code work in an external JavaScript file?

       

      Any help would be appreciated.

       

      Thanks

       

       

      Linda

        • 1. Re: Jump to a specific bookmark using JavaScript
          George_Johnson MVP & Adobe Community Professional

          Did you place that code as-is in a folder-level JavaScript file, or did you include it in a function and called the function? It should be the latter, and you should pass the doc object to it. For example:

           

          function execute_MS_bookmark(doc) {

           

          // Jump to "Monthly Summary"

          for (var i = 0; i < doc.bookmarkRoot.children.length; i++) {

              if (doc.bookmarkRoot.children[i].name.indexOf("Monthly Summary") != -1) {

                  doc.bookmarkRoot.children[i].execute();

              }

          }

           

          }

           

           

          And call it like this from the document:

           

          execute_MS_bookmark(this);

          • 2. Re: Jump to a specific bookmark using JavaScript
            lindaeliseruble Level 1

            Hello George,

             

            Are you telling me to put the following code in a folder-level JavaScript:

             

            function execute_MS_bookmark(doc) {

                   // Jump to "Monthly Summary"

                   for (var i = 0; i < doc.bookmarkRoot.children.length; i++) {

                         if (doc.bookmarkRoot.children[i].name.indexOf("Monthly Summary") != -1) {

                                doc.bookmarkRoot.children[i].execute();

                         }

                   }

            }

             

            And are you telling me to call the function "execute_MS_bookmark(this);" from the document, perhaps attaching the function call to a button click?

             

             

            Adding such a button to the document is not an option.

             

             

            Isn't there another way to do this?

             

             

            Thanks

             

             

            Linda

            • 3. Re: Jump to a specific bookmark using JavaScript
              gkaiseril MVP & Adobe Community Professional

              Since a bookmark only exist as an object in a PDF file how can it work as written as a folder level script without any control. All folder level scripts are run when the Acrobat/Reader application is started and possibly before any PDF file is opened. So your code will run but not find any bookmarks.

              Have you tired to run this code when there is no open PDF file?

               

              I get the following error when there is no open PDF:

               

              this.bookmarkRoot is undefined

              3:Console:Exec

              TypeError: this.bookmarkRoot is undefined

              3:Console:Exec

              undefined

               

              The "undefined" means there is no bookmarkRoot defined or found. Note bookmarks only exist within a PDF file that have bookmarks.

               

              Next, once you get a PDF open, how are you going to get this code to run for the form when there is no way to identify the block of code to run?

               

              As George has suggested you need to create/define a function and then call that defined function form within the PDF. Once a function is defined, that function can be called by the name of the function as part of the document level function for a PDF form, or as a field or bookmark action.

               

              Since you have decided to place this code in the user's Acrobat JavaScript folder, how are other users going to call this code located on your system. Using your approach will require all users need to install your code on their system. If you were to put the code in the PDF as a document level script, the script it could execute upon opening the PDF. If one put it in as a document level function, then it could be called upon opening the PDF, as a bookmark, as a button, etc.

               

              I would look for a function to open any bookmark in the PDF by bookmark name by searching for the existence of that named bookmark. With this approach, I could use the function in any PDF and execute any possible bookmark as needed.

              • 4. Re: Jump to a specific bookmark using JavaScript
                George_Johnson MVP & Adobe Community Professional

                It sounds like you may be trying to get that bookmark opened whenever a document with it is opened. If so, that sort of thing isn't going to work.

                • 5. Re: Jump to a specific bookmark using JavaScript
                  lindaeliseruble Level 1

                  Yes, I would like to open document to that bookmark when the script is run.  hat is only one of a number of things that need to happen. I can actually make it work, however, by doing so, nothing else happens which includes generating a results page.  In light of this, I will do without the jump to bookmark feature.  Or perhaps I can attach the bookmark feature to a button on the result page that will close the results page and call up the bookmark at the same time?

                   

                  Just a thought.

                   

                  Linda

                  • 6. Re: Jump to a specific bookmark using JavaScript
                    gkaiseril MVP & Adobe Community Professional


                    Your original question was about executing a specific bookmark. Now you are adding more issues about a calculation.

                     

                    First I would open the JavaScript console and see if there is an error.

                     

                    Next I would look to see if there is any data in the fields to summarize.

                     

                    Opening a blank form and going to the monthly summary will display the summary of the blank data.

                     

                    Have you tried adding data to the form fields.

                     

                    You might want to review the Acrobat JavaScript API Reference for how form field actions work and the sequence in which they are executed.