UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Adding Bookmarks To Pdf, Access 2010    
 
   
SerranoG
post Apr 28 2017, 09:22 AM
Post#1


UtterAccess VIP
Posts: 2,221
Joined: 1-December 03
From: Lansing, MI USA


I have Adobe Acrobat X Pro on my computer... the full version, not just the reader.

I have a long list of permits in a table that I created. I ran a query that creates an index starting with the numerals 0-9 and then letters A-Z. For each character, I find the first, e.g. company name, that starts with that character and place it in a "Bookmark Index" table. So, for example, for "A" I have "Acme Company", "B" is "Battle Creek Industries, etc.

Then I output a report to PDF, open it, set its metadata, and attempt to bookmark it. The desired result is a PDF that opens with bookmarks showing. When the person clicks, say, "F", the page drops down to "Fabrication, Inc." The code below does everything I want except for one thing. The bookmarks are there (0-9 & A-Z), but when clicked, they all go to Page 1. Apparently, my code is putting the bookmark in there, but it's not setting the location within the PDF. The location remains at Page 1.

This is my first time attempting to bookmark a PDF via VBA. Obviously, I'm not understanding the nuances. Can anyone help? Internet searches are not yielding useable examples.

thanks.gif
Go to the top of the page
 
SerranoG
post May 2 2017, 10:52 AM
Post#2


UtterAccess VIP
Posts: 2,221
Joined: 1-December 03
From: Lansing, MI USA


No takers yet, eh?

Well, I've tried a few things to no avail. I discovered that this line doesn't do anything in this context. So I deleted it.

CODE
fBM = PDBookmark.Perform(AVDoc)


I'm wondering if the problem isn't in the FINDTEXT statement.

CODE
fBM = AVDoc.FindText(rstBMIndex("strEntry"), False, False, False)


I know that it's finding the text, but when it does, does it actually move the pointer in the PDF to that location? If not, perhaps that's why all the bookmarks are pointing to Page 1. The other thought is that perhaps I can use the AcroApp.MenuItemExecute method to find the text and maybe that will move the pointer. The problem is that I cannot find anywhere that tells me what the variables are to place into that command other than "NewBookmark". The API handbook is extremely cryptic and, therefore, of very little help.
Go to the top of the page
 
SerranoG
post May 2 2017, 04:06 PM
Post#3


UtterAccess VIP
Posts: 2,221
Joined: 1-December 03
From: Lansing, MI USA


Okily-dokily... I got it to work after a lot of trial and error. As I said, this is my first attempt to bookmark a PDF via VBA. Please bear with me as this solution may seem to be rough for some of you with far better expertise than I.

The Adobe SDK is not the easiest nor the most intuitive thing to work with. However, I did realize that Adobe seems to have put their functions to different objects than just give one the ability to do logical things. For example, the CAcroPDDoc object is not endowed with the powers to find a page. That falls to the AcroAVPageView object. You'd think that once you have the document, you can simply go to a page in it. So you cannot do this:

CODE
fBM = PDDoc.GoToPage(lngPageNo)


That's too logical for them, so you have to declare a Page View object then use that instead.

CODE
fBM = AVPageView.GoTo(lngPageNo-1)


Given that the FindText doesn't set the page upon finding the text in the PDF, I had to manually set the page for each bookmark. In order to do that, I put code in the ON FORMAT of the reports' details sections that when the first item in the tblBMIndex tables are found per bookmark, it also stores the page number in the report for that bookmark. So, for example, when it finds the first company starting with the letter "P" then it stores page no. 117 associated with it. After using FindText, I go to that page using AVPageView THEN set the bookmark.

The final code that works is below.

CODE
Public Sub PDFProp(strPath As String, strReport As String, bytBMIndex As Byte)

     'Sets a PDF's metatadata and bookmarks for the given path.
    Dim strFile As String, strTitle As String, fBM As Boolean
    Dim AcroApp As CAcroApp, AVDoc As CAcroAVDoc, PDDoc As CAcroPDDoc, PDBookmark As AcroPDBookmark, AVPageView As Acrobat.AcroAVPageView
    Dim cnnMe As New ADODB.Connection, rstBMIndex As New ADODB.Recordset, strBMIndex As String

    'Set Bookmark Index
    strBMIndex = "tblBMIndex" & CStr(bytBMIndex)
    
    'Open the PDF.
    Set AcroApp = CreateObject("AcroExch.App")
    AcroApp.Hide
    
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set PDBookmark = CreateObject("AcroExch.PDBookmark", "")
    
    If Right(strPath, 1) = "\" Then
        strFile = Trim(Dir(strPath & strReport & "*.pdf"))
    Else
        strFile = Trim(Dir(strPath & "\" & strReport & ".pdf"))
    End If
    
    'Open the PDF and set its metadata.
    If Right(strPath, 1) = "\" Then
        Call AVDoc.Open(strPath & strFile, "")
    Else
        Call AVDoc.Open(strPath & "\" & strFile, "")
    End If
    
    Set AVDoc = AcroApp.GetActiveDoc
    
    If AVDoc.IsValid Then
        Set PDDoc = AVDoc.GetPDDoc
        Set AVPageView = AVDoc.GetAVPageView
        
        'Fill in PDF properties.
        strTitle = "Active PTIs Approved since 2001 Sorted by "
        
        Select Case bytBMIndex
            Case 0
                strTitle = strReport
            Case 1
                strTitle = strTitle & "Company"
            Case 2
                strTitle = strTitle & "County & City"
            Case 3
                strTitle = strTitle & "State Registration No. (SRN)"
        End Select
        
        fBM = PDDoc.SetInfo("Title", strTitle)
        fBM = PDDoc.SetInfo("Author", "MDEQ Air Quality Division")
        fBM = PDDoc.SetInfo("Subject", strTitle)
        
        If bytBMIndex = 0 Then
            fBM = PDDoc.SetInfo("Keywords", "AQD, air, permits, PTI, pending, applications")
        Else
            fBM = PDDoc.SetInfo("Keywords", "AQD, air, permits, PTI, active, approved")
        End If
        
        'Set Bookmarks
        fBM = PDDoc.SetPageMode(PDUseBookmarks)
        
        Set cnnMe = CurrentProject.Connection
        
        rstBMIndex.Open strBMIndex, cnnMe, adOpenStatic, adLockReadOnly
        rstBMIndex.MoveFirst
        
        'Starting with numerals 0 through 9, and then the letters A through Z,
        'cycle through the bookmark index table which lists the first instances (Company, County, or SRN)
        'starting with that character.  Next, find it in the PDF and then bookmark the PDF at that page.
        
        Do Until rstBMIndex.EOF
            'Find the location of where the bookmark should go.
            fBM = AVPageView.GoTo(rstBMIndex("lngPageNo") - 1)
                
            'Create the bookmark.
            fBM = AcroApp.MenuItemExecute("NewBookmark")
            fBM = PDBookmark.GetByTitle(PDDoc, "Untitled")
            fBM = PDBookmark.SetTitle(rstBMIndex("strBM"))
            
            rstBMIndex.MoveNext
        Loop
        
        rstBMIndex.Close
        cnnMe.Close
        
        Set rstBMIndex = Nothing
        Set cnnMe = Nothing
        
        'Save & Close the PDF
        If Right(strPath, 1) = "\" Then
            fBM = PDDoc.Save(1, strPath & strFile)
        Else
            fBM = PDDoc.Save(1, strPath & "\" & strFile)
        End If
    
        PDDoc.Close
    End If
    
    'Close the PDF
    fBM = AVDoc.Close(True)
    AcroApp.Exit
    
    'Cleanup
    Set AVPageView = Nothing
    Set PDBookmark = Nothing
    Set PDDoc = Nothing
    Set AVDoc = Nothing
    Set AcroApp = Nothing

End Sub
Go to the top of the page
 
Dan Dungan
post May 2 2017, 05:18 PM
Post#4



Posts: 316
Joined: 20-July 10
From: chatsworth, ca


Congratulations on sorting it out. I looked at the SDK and could not understand it.
Go to the top of the page
 
HiTechCoach
post May 7 2017, 02:32 PM
Post#5


UtterAccess VIP
Posts: 19,009
Joined: 29-September 03
From: Oklahoma City, Oklahoma


Great job figuring this out.

Thanks for paying it forward by sharing your solution. notworthy.gif
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    15th June 2019 - 10:20 PM