7 Replies Latest reply on Dec 10, 2014 10:31 AM by gpops62

    Script to Split PDF files on Bookmarks


      I'm totally new to Java scripting.  What I'm looking for is a script I can run from batch processing with professional that will look at a collection files in s directory and split ech one of them on their bookmarks into smaller files.


      The bookmarks are all based on the page so it is possible to use the bookmark object's "execute" method to execute the bookmark action and pickup the page number.


      I read that last part somewhere. If someone can give me a reference to a thread or just some code to get me started that would help. Thanks,



        • 2. Re: Script to Split PDF files on Bookmarks
          jc2po3 Level 1

          Thanks for the thread, it's not exectly what I need it has to do with VBA.  Here is what I have so far after doing some more looking around the web.


          /* Gather Bookmarks into an Array */
          // Begin job setup
          if ( typeof global.bmArray == "undefined" ) {
          global.indexCnt = 0;
          global.bmFileCnt = 0;
          global.bmArray = new Array();
          global.bmFileCnt++; // Count files
          // Get the path of the file Acrobat has taken from the selected folder
          var path2file = this.path;
          // Regular expression that acquires the base name of file
          var re = /.*\/|\.pdf$/ig;
          // filename is the base name of the file Acrobat is working on
          var filename = path2file.replace(re,"");
          // If the title is available, use it for a bookmark, else use filename
          if ((this.info.Title==null) || (this.info.Title.replace(/\s/g,"")==""))
          var bmToFile = filename;
          var bmToFile = this.info.Title;
          global.bmArray[global.indexCnt] = [bmToFile,filename];



          //Loop Throught he array
          var arLen=bmarray.length;
          for ( var i=0, len=arLen; i<len; ++i )


          >>>>>NEED CODE




          I have the code that builds the array out of the bookmarks and the code to loop throught the array.  I figured out how to extract pages when I have their numbers, so the only thing I need to know is the syntax for the execute method and for the page number that it goes to when executed.


          Then I can loop through all the bookmarks and with two variables extract pages between each bookmark.


          You help is appreciated. Thanks,



          • 3. Re: Script to Split PDF files on Bookmarks
            Bernd Alheit Adobe Community Professional & MVP

            You can find the syntax for the execute method in the Acrobat JavaScript Reference.

            • 4. Re: Script to Split PDF files on Bookmarks
              ReinhardF Level 3

              ....  it's not exectly what I need it has to do with VBA ...


              Attached a VBS example. You can rewrite it or use the command-line option and analyse the output for that what you want.


              HTH, Reinhard





              '//Read Bookmarks with pagenumber and level to CSV.
              path = "c:\TestBm.pdf"   '->Testfile or use commandline-option

              set oWs = CreateObject ("Wscript.Shell")
              set fs = CreateObject("Scripting.FileSystemObject")
              oWs.run "Acrobat.exe"
              While not oWs.AppActivate("Adobe Acrobat") : Wscript.Sleep 1000 : Wend

              Set objArgs = WScript.Arguments
              if objArgs.Count = 1 then path = ObjArgs(0)

              Info = "Read the Bookmarks (Page;Level;Text) from PDF-File to CSV-File"&VbCr&_
                     "File Name: "&Path &" Start now?"
              OK = MsgBox(Info, vbQuestion+vbYesNo,"Read Bookmarks") : if OK = vbNo then WScript.quit

              Set gApp = CreateObject("AcroExch.App")
              Set AVDoc = CreateObject("AcroExch.AVDoc")
              If (AVDoc.Open(Path,"")) Then
                Set AVDoc = gApp.GetActiveDoc()
                Set PDDoc = AVDoc.GetPDDoc()
                Set JSO = PDDoc.GetJSObject

                         '// Write some bookmark JS to a variable
              Ex = "  var vTOC = """";  "&vbLF _
                & "  function DumpBookmark(bm, nLevel)  "&vbLF _
                & "  {  "&vbLF _
                & "  var s = """";  "&vbLF _
                & "  for (var i = 0; i < nLevel; i++)  "&vbLF _
                & "      s += """";  //-> insert one space in the middle to view hierarchy "&vbLF _
                & "      if (nLevel >0){  "&vbLF _
                & "        bm.execute();  "&vbLF _
                & "        p = this.getPageLabel(this.pageNum);  "&vbLF _
                & "        l = nLevel -1;  "&vbLF _
                & "        vTOC += (+p +"";"" +l +"";"" +s + bm.name +""|"");}  "&vbLF _
                & "      if (bm.children != null)  "&vbLF _
                & "        for (var i = 0; i < bm.children.length; i++)  "&vbLF _
                & "        DumpBookmark(bm.children[i], nLevel + 1);  "&vbLF _
                & "  }  "&vbLF _
                & "  DumpBookmark(this.bookmarkRoot, 0);  "&vbLF _
                & "  //app.alert(vTOC,3);    "

                 '  // Execute the JavaScript
                Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
                 AForm.Fields.ExecuteThisJavaScript EX

                 '  // Write Bookmarks to CSV  (PageNr;Level;Text)
              Set JSO = PDDoc.GetJSObject    '//->renew JSO for some unknown reasons
              xf = jso.vToc
              CsvFN = left(path, InstrRev(path, ".")-1) &"_bm.csv"
              set FL = fs.OpenTextFile(CsvFN, 2, true)          '//-> 2=ForWriting
              xWr = split(xf,"|")
              for x=0 to UBound(xWr)-1
                FL.WriteLine xWr(x)
                  call oWs.run("Notepad.exe """& CsvFN &"""",,true)
                MsgBox "Failed to Open: " & Path
              End If

              Set JSO = Nothing
              Set PDDoc = Nothing
              Set AVDoc = Nothing
              Set AForm = Nothing
              Set gApp = Nothing

              • 5. Re: Script to Split PDF files on Bookmarks
                try67 MVP & Adobe Community Professional

                I developed a script that does exactly that: http://try67.blogspot.com/2008/12/acrobat-extract-chapters-by-bookmarks.html

                If you're interested, contact me by PM or email.

                • 6. Re: Script to Split PDF files on Bookmarks
                  jc2po3 Level 1

                  Yes, it looks like you have the script That I need I'm just trying to do it without paying for it. Thanks,



                  • 7. Re: Script to Split PDF files on Bookmarks

                    5 years later... hopefully this helps someone out there.


                    I couldn't find much information online about this myself, but hammered away until it worked. My code is for a slightly different purpose, but you should easily be able to adjust it for your needs.


                    My sources:


                    Private Sub extractBookmark()


                    Dim AcroApp As AcroApp, AVDoc As AcroAVDoc, PDDoc As AcroPDDoc, PDBookmark As AcroPDBookmark, AVPageView As AcroAVPageView

                    Dim newPDF As AcroPDDoc, mergePDF As AcroPDDoc

                    Dim jso As Object, BookMarkRoot As Object

                    Dim masterPath As String, testName As String, i As Variant, bookmark As Variant

                    Dim startN As Integer, endN As Integer, nPages As Integer, totalP As Integer


                    Set AcroApp = CreateObject("AcroExch.App")

                    Set AVDoc = CreateObject("AcroExch.AVDoc")

                    Set PDDoc = CreateObject("AcroExch.PDDoc")

                    Set PDBookmark = CreateObject("AcroExch.PDBookmark")


                    masterPath = ActiveWorkbook.Path & "\MasterDocument.pdf"     'Path of the PDF containing the bookmarks to be extracted


                    AVDoc.Open masterPath, vbNull


                    Set AVPageView = AVDoc.GetAVPageView

                    Set PDDoc = AVDoc.GetPDDoc

                    Set jso = PDDoc.GetJSObject

                    Set BookMarkRoot = jso.BookMarkRoot


                    bookmark = jso.BookMarkRoot.Children

                    totalP = PDDoc.GetNumPages


                    'Loop through the 10 bookmarks in MasterDocument.pdf and get the starting page # of each. This # has to be changed to the # of bookmarks in your pdf. Or write something to count the bookmarks and use that variable. "productName" is a global variable for my specific project... You'll need to revise this for your purpose.


                    For i = 0 To 9

                        If bookmark(i).Name = productName Then

                            PDBookmark.GetByTitle PDDoc, bookmark(i).Name

                            PDBookmark.Perform AVDoc


                            startN = AVPageView.GetPageNum

                            If i < 9 Then

                                PDBookmark.GetByTitle PDDoc, bookmark(i + 1).Name

                                PDBookmark.Perform AVDoc


                                endN = AVPageView.GetPageNum

                                nPages = endN - startN


                                nPages = totalP - startN

                            End If

                        End If





                    'This part will also need to be modified. I am inserting the bookmarks into an already made PDF (global variable for me "fName"). You'll want to create a new pdf.


                    Set newPDF = CreateObject("AcroExch.PDDoc")

                    Set mergePDF = CreateObject("AcroExch.PDDoc")


                    newPDF.Open fName & ".pdf"     'This line needs changing for your purpose. Probably could be.... newPDF.Create

                    mergePDF.Open masterPath


                    newPDF.InsertPages 0, mergePDF, startN, nPages, 0

                    newPDF.Save PDSaveFull, fName & ".pdf"





                    End Sub