Public Sub Find_Text_in_PDF_Add_Adjacent_Image()
Dim PDDoc As Acrobat.AcroPDDoc
Dim JSO As Object
Dim imageField As Object
Dim PDFinputFile As String, PDFoutputFile As String
Dim imageFile As String
Dim page As Long, word As Long
Dim quads As Variant, fieldRect(0 To 3)
Dim wordText As String
PDFinputFile = "C:\path\to\Your PDF.pdf"
PDFoutputFile = "C:\path\to\Your PDF WITH IMAGES ADDED.pdf"
imageFile = "C:\path\to\Your image.jpg"
Set PDDoc = New Acrobat.AcroPDDoc
If PDDoc.Open(PDFinputFile) Then
Set JSO = PDDoc.GetJSObject
For page = 0 To PDDoc.GetNumPages() - 1
For word = 0 To JSO.getpageNumWords(page) - 1
wordText = JSO.getpageNthWord(page, word, True)
If wordText = "TheText" Then
quads = JSO.getPageNthWordQuads(page, word)
fieldRect(0) = CLng(quads(0)(2) + 10)
fieldRect(1) = CLng(quads(0)(1))
fieldRect(2) = CLng(fieldRect(0) + PixelsToPoints(130))
fieldRect(3) = CLng(fieldRect(1) - PixelsToPoints(140))
Set imageField = JSO.addField("button" & word + 1, "button", page, fieldRect)
On Error Resume Next
imageField.buttonImportIcon imageFile
On Error GoTo 0
imageField.buttonPosition = JSO.Position.iconOnly
imageField.readOnly = True
End If
Next
Next
If PDDoc.Save(Acrobat.PDSaveFlags.PDSaveFull, PDFoutputFile) Then
MsgBox "Successfully saved " & PDFoutputFile
Else
MsgBox "Cannot save the output PDF document " & PDFoutputFile, vbExclamation, "Find Text and Add Image"
End If
PDDoc.Close
End If
Set PDDoc = Nothing
End Sub
Private Function PixelsToPoints(pixels As Long, Optional DPI As Long = 96) As Double
PixelsToPoints = pixels / DPI * 72
End Function