14 Replies Latest reply on Oct 25, 2012 3:43 PM by Jo_2005

    Split/Extract pages by bookmarks

    JoFrmOZ

      I have created a script which will successfully split the pdf at the first bookmark.

       

      I need for the script to split all bookmarks from the pdf. 

       

      I have modifed the script but the console reports the following error:

      TypeError: bookmarkRoot.children[i] is undefined

       

      Can someone please provide assistance to modify the script to split all bookmarks from the pdf?

       

      Script to split the pdf at the first bookmark - this works!

       

      if (this.bookmarkRoot.children!=null)
      {
      var bm = bookmarkRoot.children[0].execute();
      var bmname = bookmarkRoot.children[0].name;
      this.extractPages({nStart:bm, cPath: "/C/Temp/"+bmname+".pdf"});
      }

       

      Script to split all bookmarks in the pdf - this does not work!

       

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

      var bm = bookmarkRoot.children[i].execute();
      var bmname = bookmarkRoot.children[i].name;
      this.extractPages({nStart:bm, cPath: "/C/Temp/"+bmname+".pdf"});

      }

       

      Thank you!

        • 1. Re: Split/Extract pages by bookmarks
          BarlaeDC Level 4

          Hi,

           

          Can you let us know when you are running this as I was able to run your code from the console and it work as I would have expected?

           

          The error you are getting is normally caused by removing a bookmark, as the bookmark tree updates dynamically and this means that if you remove a node you have to adjust your loop.

           

          Hope this helps

           

          Malcolm

          • 2. Re: Split/Extract pages by bookmarks
            JoFrmOZ Level 1

            Thanks for your reply, Malcolm.

             

            This is the script I am running in the console:

             

            if (this.bookmarkRoot.children!=null)
            {
            for (var i=0; i < this.bookmarkRoot.children.length; i++)
            var bm = bookmarkRoot.children[i].execute();
            var bmname = bookmarkRoot.children[i].name;
            this.extractPages({nStart:bm, cPath: "/C/Temp/"+bmname+".pdf"});
            }

             

            The error reporting back in the console:

             

            BookmarkRoot.children[i] is undefined
            5:Console:ExecException in line 5 of function top_level, script Console:Exec

            TypeError: bookmarkRoot.children[i] is undefined
            5:Console:Exec
            undefined

             

            Any thoughts, most appreciated!

            • 3. Re: Split/Extract pages by bookmarks
              Bernd Alheit Adobe Community Professional & MVP

              Use:

               

              this.bookmarkRoot.children[i]

              1 person found this helpful
              • 4. Re: Split/Extract pages by bookmarks
                JoFrmOZ Level 1

                Thank you for your assistance.

                 

                Script revised as suggested, same console error still showing, line 5 for: var bmname = this.bookmarkRoot.children[i].name;

                if (this.bookmarkRoot.children!=null)
                {
                for (var i=0; i < this.bookmarkRoot.children.length; i++)
                var bm = this.bookmarkRoot.children[i].execute();
                var bmname = this.bookmarkRoot.children[i].name;
                this.extractPages({nStart:bm, cPath: "/C/Temp/"+ bmname +".pdf"});
                }

                 

                Can you please advise what could be wrong with var bmname = this.bookmarkRoot.children[i].name;?

                Thanks.

                • 5. Re: Split/Extract pages by bookmarks
                  try67 MVP & Adobe Community Professional

                  You need to create a block after the for-command, ie. use curly brackets to

                  mark the code that should be executed in the loop.

                  • 6. Re: Split/Extract pages by bookmarks
                    JoFrmOZ Level 1

                    Thank you for your advice.

                     

                    I have placed the curly brackets after the "for command" and the script will rename the split files as the bookmark names.

                     

                    There is a problem with the var bm = this.bookmarkRoot.children[i].execute(); part of the script.

                     

                    The script splits all the bookmarks at page 1 and not the correct page associated with the bookmark names.

                     

                    My thoughts are that the bookmark execute command may need to be recursive to work??

                     

                    The script has been modified for recursive and the console reports back as undefined and does not split the pdf at the bookmarks.

                     

                    Can you please advise if I am going in the right direction in getting the file to split correctly:

                     

                    function splitBookmarks (bm,  nLevel)
                    {
                    if (bm.children!=null)
                    {
                    for (var i=0; i < bm.children.length; i++)
                    {
                    var bmd = splitBookmarks(bm.children[i], nLevel + 1);
                    var bme = bmd.execute();
                    var bmname =bm.children[i].name;
                    this.extractPages({nStart:bme, cPath: "/C/Temp/"+ bmname +".pdf"});
                    }
                    }
                    }

                    Thanks!

                    • 7. Re: Split/Extract pages by bookmarks
                      George_Johnson MVP & Adobe Community Professional

                      I haven't read through all of the code, but it seems to be dependent on the bookmark execute method returning something, like a page number. It doesn't.

                      • 8. Re: Split/Extract pages by bookmarks
                        JoFrmOZ Level 1

                        Hi George,

                         

                        Can you please shed some light on how to get the bookmark execute to return the page from the book mark action?

                         

                        Your thoughts will be most appreciated.

                         

                        Thank you.

                        • 9. Re: Split/Extract pages by bookmarks
                          George_Johnson MVP & Adobe Community Professional

                          After you execute the bookmark, which presumably has an action that opens a particular page, the document will go to that page. You can then get the page number using the pageNum property, which is zero-based. So the first page is page 0, the second page is 1, etc.

                          1 person found this helpful
                          • 10. Re: Split/Extract pages by bookmarks
                            JoFrmOZ Level 1

                            Thank you for your advice you have been of great help.

                             

                            I have used the pageNum property with the bookmark execute and the script will split the pdf at the bookmarks on the correct page number.

                            • 11. Re: Split/Extract pages by bookmarks
                              Danny Noonan

                              Hey JoFrmOZ,

                              Could you please post your final script if you get a chance? We could use something like this.

                              Thanks in advance

                              Dan

                              • 12. Re: Split/Extract pages by bookmarks
                                JoFrmOZ Level 1

                                Hi Dan,

                                 

                                Script to split/extract pages by bookmarks as follows:

                                 

                                if (this.bookmarkRoot.children!=null)

                                {

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

                                {

                                var bm = this.bookmarkRoot.children[i].execute();

                                var bmname = this.bookmarkRoot.children[i].name;

                                this.extractPages({nStart:bm + this.pageNum++,  cPath: "/C/Temp/"+ bmname +".pdf"});

                                }

                                }

                                 

                                 

                                • 13. Re: Split/Extract pages by bookmarks
                                  EngSq

                                  This works great if there are no sub bookmarks, but if there are, how can we allow for that?  Some of our bookmarks have 2 or more pages under them, but this script only grabs the top one.

                                  • 14. Re: Split/Extract pages by bookmarks
                                    Jo_2005 Level 1

                                    Unable to provide an answer with the problem for the sub bookmarks.

                                     

                                    You will need to start a new discussion for help with this.