15 Replies Latest reply: Dec 12, 2014 1:59 PM by gpops62 RSS

    Split PDF Files by Bookmarks

    Community Member
      Can anyone please let me know how do I split PDF Files by bookmark using Java Script.

      I am making a Visual Basic utility to read the bookmarks in PDF Files and split them based on the bookmarks. I am able to split the PDF Files by using Arts PDF Split Pro. However since I am already using Adobe Standard, I would like to do it with Adobe itself.

      Thus, I pay license for only one software and not two.

      Thanks
      Rahul
        • 1. Re: Split PDF Files by Bookmarks
          GKaiseril CommunityMVP
          Coding JavaScripts in Standard can be done, but it is not easy.

          This can be done if and only if the bookmarks you are using for the split go to a page, by using the bookmark "execute" method.

          You are aware bookmarks can do far more then go to a page?

          In Acrobat JavaScirpt one can not see the actions associated with a bookmark or field even though one can set the script action through JavaScript.
          • 2. Re: Split PDF Files by Bookmarks
            Community Member
            Thanks for your reply Geo.

            Can you please post the code for this.
            • 3. Re: Split PDF Files by Bookmarks
              (Aandi_Inston) Community Member
              I think writing the code will be an interesting project for you. We
              can help you write JavaScript, but I regret that you may find little
              interest in doign the code for you.

              Aandi Inston
              • 4. Re: Split PDF Files by Bookmarks
                Community Member
                Thanka for the reply. By writing the code I meant the help with Javascript itself.
                • 5. Re: Split PDF Files by Bookmarks
                  Patrick Leckey Community Member
                  Well why don't you show us what you've got and explain what parts you don't understand so that we can help you better understand the method?

                  What Aandi meant is that there are very few people who will simply post code for you to take and use. Not a lot of people enjoy giving away work without getting paid for their efforts. We'll help you with the JavaScript you have, but not write the JavaScript for you.
                  • 6. Re: Split PDF Files by Bookmarks
                    Community Member
                    I have the following line of Code. However I have no clue how to use the Execute method to split the PDF. Please if anyone can help me with this.
                    I have removed some of the lines in code for simplicity.

                    >Private Sub SplitPDF()
                    >
                    > Dim PDFDoc As Object
                    >
                    > Dim JSO As Object
                    >
                    > Dim PDFCompletePath As String
                    >
                    > PDFCompletePath = "C:\Documents and Settings\ragarwal\My Documents\My Docs\PDFTest\Test.pdf"
                    >
                    > Dim BookMarkRoot As Object
                    >
                    > Dim BookMarks As Variant
                    >
                    > Dim LevelCount As Long
                    >
                    > Set PDFDoc = CreateObject("AcroExch.PDDoc")
                    >
                    > Stop
                    >
                    > If PDFDoc.Open(PDFCompletePath) Then
                    >
                    > Set JSO = PDFDoc.GetJSObject
                    >
                    > BookMarks = JSO.BookMarkRoot.Children
                    >
                    > LevelCount = UBound(BookMarks)
                    >
                    > Dim FirstNode
                    >
                    > FirstNode = BookMarks(0).Children
                    >
                    > Dim i As Integer
                    >
                    > Dim CurrentRootNode
                    >
                    > For i = 0 To LevelCount
                    >
                    > CurrentRootNode = BookMarks(i).Children
                    >
                    > BookMarks(i).Execute
                    >
                    > Next
                    >
                    > End If
                    >
                    > Exit Sub
                    >
                    >End Sub
                    • 7. Re: Split PDF Files by Bookmarks
                      (Aandi_Inston) Community Member
                      There is no method to find out the page for a bookmark directly. (A
                      bookmark might not even go to a page).

                      But if your bookmarks are all "normal" (for example, you don't have a
                      bookmark to open a new file), you can use the Execute method. After
                      using Execute the current page of the document will tell you,
                      indirectly, where the bookmark went. You can then record all of this
                      information and use it later.

                      Aandi Inston
                      • 8. Re: Split PDF Files by Bookmarks
                        Community Member
                        Thanks Aandi for your reply.
                        However, what I am mainly interested is finding a way to split the PDF Files using bookmarks.
                        All bookmarks in my PDF Files corresponds to a page.

                        What I understand from your reply is, by using the Execute method, I can go to the page in the Bookmark. But how do I split the PDF.

                        Does Adobe provides a standard method wherein I can split all the pages in a PDF file using a bookmark.
                        I am able to do this using ArtsPro PDF Splitter or PDSplit.

                        Rahul
                        • 9. Re: Split PDF Files by Bookmarks
                          (Aandi_Inston) Community Member
                          >What I understand from your reply is, by using the Execute method, I can go to the page in the Bookmark. But how do I split the PDF.

                          Some combination of creating a new document, PDDoc.InsertPages, and
                          saving files.
                          >
                          >Does Adobe provides a standard method wherein I can split all the pages in a PDF file using a bookmark.

                          No.

                          >I am able to do this using ArtsPro PDF Splitter or PDSplit.

                          That's why they sell it...!

                          Aandi Inston
                          • 10. Re: Split PDF Files by Bookmarks
                            GKaiseril CommunityMVP
                            Within a PDF document and knowing the bookmark names to use for the extraction, one can use "bookmark" object's "execute" method to get the page associated with that book mark and then use the "doc" object's "extractPages" method to extract the pages into another PDF. See the Acrobat JavaScript Reference for more information and security restrictions on using path names in methods.
                            • 11. Re: Split PDF Files by Bookmarks
                              GKaiseril CommunityMVP

                              And that is a standalone program and not a user programmable JavaScript within Acrobat. Acrobat JavaScript can not access the code of a bookmark so there is no direct way for Acrobat JavaScript to see the action of the book mark, but other programing languages can see the action of the book marks. So the only sure way to split a PDF by the page action of a bookmark is with a 3rd party plug-in or 3rd party program. Arts PDF Split & Merge can also provide additional features.

                              • 12. Re: Split PDF Files by Bookmarks
                                try67 CommunityMVP

                                That's not completely accurate. I have created a JavaScript tool for Acrobat

                                that can split a PDF by top-level bookmarks (or chapters, as I call it). See

                                here:

                                http://try67.blogspot.com/2008/12/acrobat-extract-chapters-by-bookmarks.html

                                • 13. Re: Split PDF Files by Bookmarks
                                  GKaiseril CommunityMVP

                                  And the PDF you chose has only bookmarks that go to a specific page in the PDF.

                                   

                                  What about bookmarks that open web pages, run a JavaScirpt like an 'app.alert'?

                                  • 14. Re: Split PDF Files by Bookmarks
                                    try67 CommunityMVP

                                    My script ignores bookmarks that don't lead to a certain page. Obviously if

                                    the the bookmark's scripts causes a modal dialog like app.alert, the script

                                    will be stuck until that dialog is closed. It's up to the user to know which

                                    kinds of bookmarks their file has.

                                    • 15. Re: Split PDF Files by Bookmarks
                                      gpops62 Community Member

                                      I couldn't find much information online about this myself, but hammered away until it worked. 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

                                              AcroApp.Exit

                                              startN = AVPageView.GetPageNum

                                              If i < 9 Then

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

                                                  PDBookmark.Perform AVDoc

                                                  AcroApp.Exit

                                                  endN = AVPageView.GetPageNum

                                                  nPages = endN - startN

                                              Else

                                                  nPages = totalP - startN

                                              End If

                                          End If

                                      Next

                                       

                                      PDDoc.Close

                                       

                                      '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"

                                       

                                      newPDF.Close

                                      mergePDF.Close

                                       

                                      End Sub