VBA to place images inside a PDF???

sebby_joe

New Member
Joined
Nov 24, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hey guys,
Is it possible to use VBA to place images inside an existing PDF with accuracy and precision? Even better, could VBA be used to place said images into an INDD file? If not, is there a different scripting language that is suited for such a task?

I’ve been creating print-advertising postcards for a realtor client, and the process is monotonous, so I’m desperate to automate my current workflow as much as possible.

I’d appreciate a push in the right direction or any thoughts all y’all may have.

Thank you!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This can be done with VBA and the Acrobat API, which is available only if you've installed Acrobat Pro, not the free Adobe Reader. Here's some basic code which adds the same image to the bottom left of each page in the PDF. A reference to Adobe Acrobat Type Library is needed.
VBA Code:
Public Sub Insert_Images_In_PDF()

    Dim PDFinputFile As String, PDFoutputFile As String
    Dim imageFile As String
    Dim AcrobatApp As Acrobat.AcroApp
    Dim AcroAVDocInput As Acrobat.AcroAVDoc
    Dim AcroPDDocInput As Acrobat.AcroPDDoc
    Dim jso As Object
    Dim pageRect As Variant
    Dim pageField As Object
    Dim fieldRect(0 To 3) As Double
    Dim page As Long
        
    PDFinputFile = "C:\folder\path\file.pdf"
    imageFile = "C:\folder\path\image.jpg"  'width 240 pixels, height 135 pixels
    
    PDFoutputFile = Replace(PDFinputFile, ".pdf", " WITH IMAGES.pdf")
            
    Set AcrobatApp = New Acrobat.AcroApp        'CreateObject("AcroExch.App")
    Set AcroAVDocInput = New Acrobat.AcroAVDoc  'CreateObject("AcroExch.AVDoc")
    
    If AcroAVDocInput.Open(PDFinputFile, "") Then
    
        Set AcroPDDocInput = AcroAVDocInput.GetPDDoc()
    
        Set jso = AcroPDDocInput.GetJSObject
        
        For page = 0 To AcroPDDocInput.GetNumPages() - 1
        
            'Get page boundary coordinates - could be used to calculate position of button
            pageRect = jso.getPageBox("Crop", page)
            
            'Coordinates of button to be added - top-left (x,y), bottom-right (x,y)
            fieldRect(0) = 0
            fieldRect(1) = 135
            fieldRect(2) = 240
            fieldRect(3) = 0
            
            'Add button with image to this page
            Set pageField = jso.addField("button" & page + 1, "button", page, fieldRect)
            pageField.buttonImportIcon imageFile
            pageField.buttonPosition = jso.Position.iconOnly
            pageField.ReadOnly = True
            
        Next
        
        'Save as output PDF
        
        AcroPDDocInput.Save 1, PDFoutputFile
        AcroAVDocInput.Close True
        
        If AcroAVDocInput.Open(PDFoutputFile, "") Then
            AcrobatApp.Show
            AppActivate Mid(PDFoutputFile, InStrRev(PDFoutputFile, "\") + 1), False
        End If

    Else
    
        MsgBox "Unable to open PDF input file" & vbCrLf & PDFinputFile
        
    End If

    Set AcroPDDocInput = Nothing
    Set AcroAVDocInput = Nothing
    Set AcrobatApp = Nothing

End Sub
 
Upvote 0
Solution
Holy S*#@! Thanks for this @John_w
I didn’t think that anyone would take the time to create/share code with me on such an obscure question! Now, I not only know that it can be done, but I also have a model of how to do so.

I’m a bit of a VBA newbie, so I’ll work hard to understand and manipulate what you gave me.

Deeply appreciated,
SJ
 
Upvote 0
If Cross Posting, posting the same question, on multiple forums it is only polite to mention that you did.
Also put a hyperlink to that forum in all posts in forums.
If you wonder why, imagine several people working on your problem to help you while you could have a solution in one of the other forums waiting for you.
A lot of waisted time possibly.
 
Upvote 0
If Cross Posting, posting the same question, on multiple forums it is only polite to mention that you did.
Also put a hyperlink to that forum in all posts in forums.
If you wonder why, imagine several people working on your problem to help you while you could have a solution in one of the other forums waiting for you.
A lot of waisted
If Cross Posting, posting the same question, on multiple forums it is only polite to mention that you did.
Also put a hyperlink to that forum in all posts in forums.
If you wonder why, imagine several people working on your problem to help you while you could have a solution in one of the other forums waiting for you.
A lot of waisted time possibly.
FYI, I cross-posted this question on Chandoo.org’s VBA Macros Forum as well.

Link: Can VBA be used to place images inside a PDF???
 
Upvote 0
This can be done with VBA and the Acrobat API, which is available only if you've installed Acrobat Pro, not the free Adobe Reader. Here's some basic code which adds the same image to the bottom left of each page in the PDF. A reference to Adobe Acrobat Type Library is needed.
VBA Code:
Public Sub Insert_Images_In_PDF()

    Dim PDFinputFile As String, PDFoutputFile As String
    Dim imageFile As String
    Dim AcrobatApp As Acrobat.AcroApp
    Dim AcroAVDocInput As Acrobat.AcroAVDoc
    Dim AcroPDDocInput As Acrobat.AcroPDDoc
    Dim jso As Object
    Dim pageRect As Variant
    Dim pageField As Object
    Dim fieldRect(0 To 3) As Double
    Dim page As Long
       
    PDFinputFile = "C:\folder\path\file.pdf"
    imageFile = "C:\folder\path\image.jpg"  'width 240 pixels, height 135 pixels
   
    PDFoutputFile = Replace(PDFinputFile, ".pdf", " WITH IMAGES.pdf")
           
    Set AcrobatApp = New Acrobat.AcroApp        'CreateObject("AcroExch.App")
    Set AcroAVDocInput = New Acrobat.AcroAVDoc  'CreateObject("AcroExch.AVDoc")
   
    If AcroAVDocInput.Open(PDFinputFile, "") Then
   
        Set AcroPDDocInput = AcroAVDocInput.GetPDDoc()
   
        Set jso = AcroPDDocInput.GetJSObject
       
        For page = 0 To AcroPDDocInput.GetNumPages() - 1
       
            'Get page boundary coordinates - could be used to calculate position of button
            pageRect = jso.getPageBox("Crop", page)
           
            'Coordinates of button to be added - top-left (x,y), bottom-right (x,y)
            fieldRect(0) = 0
            fieldRect(1) = 135
            fieldRect(2) = 240
            fieldRect(3) = 0
           
            'Add button with image to this page
            Set pageField = jso.addField("button" & page + 1, "button", page, fieldRect)
            pageField.buttonImportIcon imageFile
            pageField.buttonPosition = jso.Position.iconOnly
            pageField.ReadOnly = True
           
        Next
       
        'Save as output PDF
       
        AcroPDDocInput.Save 1, PDFoutputFile
        AcroAVDocInput.Close True
       
        If AcroAVDocInput.Open(PDFoutputFile, "") Then
            AcrobatApp.Show
            AppActivate Mid(PDFoutputFile, InStrRev(PDFoutputFile, "\") + 1), False
        End If

    Else
   
        MsgBox "Unable to open PDF input file" & vbCrLf & PDFinputFile
       
    End If

    Set AcroPDDocInput = Nothing
    Set AcroAVDocInput = Nothing
    Set AcrobatApp = Nothing

End Sub
Thank you for your help and the VBA code! Instead of adding the image at the bottom of the PDF, is there a way to find a specific text on every page and add the image next to that text? The position of the text to find is different on every page, and I'm not sure if there's a way to do this. Please let me know if you have any suggestions.
 
Upvote 0
Instead of adding the image at the bottom of the PDF, is there a way to find a specific text on every page and add the image next to that text? The position of the text to find is different on every page, and I'm not sure if there's a way to do this.
Yes, this should be possible.

Find the text using JSO.getPageNumWords and JSO.getPageNthWord. Then get the co-ordinates of the bounding rectangle around the found text using JSO.getPageNthWordQuads. The code in the following thread uses these methods for another purpose, but the technique should be the same for you:


Then adjust the co-ordinates to define the position of a button field containing the image using JSO.addField("button"...., as shown above.

Please start a new thread if you need more help.
 
Upvote 0

Forum statistics

Threads
1,216,137
Messages
6,129,097
Members
449,486
Latest member
malcolmlyle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top