Copy link to clipboard
Copied
Hi,
has anyone successfully read Adobe PDF Document Bookmarks and the contents of the bookmarks using Excel VBA? I use Adobe Actobat Std 2017 and Excel 2013.
Any help would be appreciated.
Athula
Copy link to clipboard
Copied
What have you tried, and what doesn't work? Or are you still looking for the Acrobat SDK (=documentation)? Or do you have the SDK but can't find the relevant methods?
Copy link to clipboard
Copied
Hi,
Thank you for replying. I am new to PDF automation with VBA. I wrote script to open PDFS. I looking for a VBA code example to either just the bookmark collection and the values in each bookmark.
Any assiatnce would be greatly appreciated.
regards
Athula
Copy link to clipboard
Copied
Ok, you MIGHT find samples, but Acrobat programming usually needs a deep study of the documentation. That's what the Acrobat SDK is - just the documentation https://www.adobe.com/devnet/acrobat/documentation.html
Copy link to clipboard
Copied
Adobe's failure to provide built-in functionality for accessing bookmarks is unconscionable.
I have a libary of 100s of sheet music PDFs and I want to create a catalog of all the songs (each song is bookmarked), so I figured out how to do this via Excel and VBA. My solution processes all of the PDFs in a user-selected folder and writes the PDF file name plus each bookmark to one sheet and dumps the name of any PDF that is missing bookmarks to a 2nd sheet (so I can go clean up the bookmarks). I'm just including the code to actually read the bookmarks, however.
Note that I only needed to process PDFs with flat bookmark structures, so I don't know what this code will do with nested bookmarks, nor do I care to figure that out (but if you do, please share so others can benefit).
Tip: in the VBA Window, under Tools>References, make sure that "Abobe Acrobat 10.0 Type libary" is checked (the version number may vary, depending on your versions of Excel and Adobe). I don't know if this will work if you do not have Adobe Acrobat; if you only have Adobe Reader, I suspect it will not.
'**************************************************
Sub Get_Bookmarks()
Dim strPDf As String
strPDf = "G:\Data\Books\Sheet Music\Catalog\To Do\Download\Mariah Carey - Number Ones.pdf"
' Create an Acrobat app
Dim AcroApp As Acrobat.CAcroApp
Set AcroApp = CreateObject("AcroExch.App")
' Open the PDF document, Acrobat Viewer layer
'
' strPDF must contain the name and path of the PDF you want to process
' you can hard-code it, prompt for it, read it from a list in Excel, etc.
Dim avDoc As Acrobat.CAcroAVDoc
Set avDoc = CreateObject("AcroExch.AVDoc")
avDoc.Open strPDf, ""
' get the Portable Document layer
Dim pdDoc As Acrobat.CAcroPDDoc
Set pdDoc = avDoc.GetPDDoc
' Create a Javascript object
Dim jso As Object
Set jso = pdDoc.GetJSObject
Dim vBookmarks As Variant
Dim strBookmark As String
' If you run this code in a loop (i.e. to process multiple PDFs), the bookmarks from the previous
' pass will persist, so set vBookmarks to Nil before trying to use BookMarkRoot. That way, if
' vBooksmarks is empty, you know there are no bookmarks in the current PDF
vBookmarks = Nil
' If the PDF has no bookmarks, BookMarkRoot.Children will cause an error, so you need to use an On Error statement
On Error Resume Next
vBookmarks = jso.BookMarkRoot.Children
' If there are no bookmarks in the current PDF, vBookmark will be empty
If Not IsEmpty(vBookmarks) Then
For Each Child In vBookmarks
strBookmark = Child.Name ' this is your bookmark text - print it, write it to an Excel sheet or CVS file, etc.
Next
End If
pdDoc.Close
avDoc.Close False
Set jso = Nothing
Set avDoc = Nothing
Set pdDoc = Nothing
Set AcroApp = Nothing
End Sub
'**************************************************