Public Sub Insert_Images_In_PDF()
Dim PDFinputFile As String
Dim PDFoutputFile As String
Dim imageFile As String
Dim i As Integer
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
Set targetWorkbook = ThisWorkbook
Set rh = targetWorkbook.Sheets("RCT")
Set sh = targetWorkbook.Sheets("Stamper")
lastrowA = rh.Cells(sh.Rows.Count, "A").End(xlUp).Row
lastrowI = rh.Cells(sh.Rows.Count, "I").End(xlUp).Row
For i = lastrowA - (lastrowA - lastrowI - 1) To rh.Range("A" & Application.Rows.Count).End(xlUp).Row
PDFinputFile = "S:\01 PV\02 Proffesion\02 Engineering & QC\Rev_1 - Acrobat PRO - Multi\01 Drawings - Projects\" & rh.Range("F" & i).Value & ".pdf"
imageFile = "S:\01 PV\02 Proffesion\02 Engineering & QC\Rev_1 - Acrobat PRO - Multi\02 Signatures\" & rh.Range("F" & i).Value & " " & "x" & " " & rh.Range("D" & i).Value & ".png" 'width 240 pixels, height 135 pixels
PDFoutputFile = "S:\01 PV\02 Proffesion\02 Engineering & QC\Rev_1 - Acrobat PRO - Multi\03 Signed Drawings\" & rh.Range("F" & i).Value & " " & "x" & " " & rh.Range("D" & i).Value & ".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) = 30
fieldRect(1) = 120
fieldRect(2) = 150
fieldRect(3) = 10
'Add button with image to this page
Set pageField = jso.addField("button" & page + 1, "button", page, fieldRect)
On Error Resume Next 'ignore "Run-time error '1001'" because the image is successfully added
pageField.buttonImportIcon imageFile
On Error GoTo 0
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), True
'End If
Else
MsgBox "Unable to open PDF input file" & vbCrLf & PDFinputFile
End If
rh.Range("I" & i).Value = 1
Next i
Set AcroPDDocInput = Nothing
Set AcroAVDocInput = Nothing
Set AcrobatApp = Nothing
End Sub
'Once I try to change for all pdfs from the folder:
Public Sub Insert_Images_In_PDF2()
Dim PDFFolder As String
Dim PDFiFile As String
Dim PDFoFile As String
Dim ImageFile As String
Dim i As Integer
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
Set targetWorkbook = ThisWorkbook
Set rh = targetWorkbook.Sheets("RCT")
Set sh = targetWorkbook.Sheets("Stamper")
lastrowA = rh.Cells(sh.Rows.Count, "A").End(xlUp).Row
lastrowI = rh.Cells(sh.Rows.Count, "I").End(xlUp).Row
For i = lastrowA - (lastrowA - lastrowI - 1) To rh.Range("A" & Application.Rows.Count).End(xlUp).Row
' There are 3-7 PDFs in the location
PDFFolder = "S:\01 PV\02 Proffesion\02 Engineering & QC\Rev_2 - Acrobat PRO - Multi\01 Drawings\" & rh.Range("F" & i).Value & "\"
PDFiFile = Dir(PDFFolder & "*.pdf")
' In the location, there are many images, but according to below - there is need to select just one for all pdfs in specified location
ImageFile = "S:\01 PV\02 Proffesion\02 Engineering & QC\Rev_2 - Acrobat PRO - Multi\02 Signatures\" & rh.Range("D" & i).Value & ".png"
'Would be nice to get orginal name of each pdf + image name and save them in the specified location
PDFoFile = "S:\01 PV\02 Proffesion\02 Engineering & QC\Rev_2 - Acrobat PRO - Multi\03 Signed Drawings\" & rh.Range("D" & i).Value & "\" ' & "?????"
Set AcrobatApp = New Acrobat.AcroApp 'CreateObject("AcroExch.App")
Set AcroAVDocInput = New Acrobat.AcroAVDoc 'CreateObject("AcroExch.AVDoc")
If AcroAVDocInput.Open(PDFiFile, "") 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) = 20
fieldRect(1) = 120
fieldRect(2) = 100
fieldRect(3) = 10
'Add button with image to this page
Set pageField = jso.addField("button" & page + 1, "button", page, fieldRect)
On Error Resume Next 'ignore "Run-time error '1001'" because the image is successfully added
pageField.buttonImportIcon ImageFile
On Error GoTo 0
pageField.buttonPosition = jso.Position.iconOnly
pageField.ReadOnly = True
Next
'Save as output PDF
AcroPDDocInput.Save 1, PDFoFile
AcroAVDocInput.Close True
Else
MsgBox "Unable to open PDF input file" & vbCrLf & PDFiFile
End If
rh.Range("I" & i).Value = 1
Next i
Set AcroPDDocInput = Nothing
Set AcroAVDocInput = Nothing
Set AcrobatApp = Nothing
End Sub