1

I'm quite new to vba coding with adobe and can't get any code to work. I first need to open a pdf located in my documents (with adobe acrobat) and then edit the 5th page by adding a textbox/or a number of text boxes. I was wondering if this was possible and if there was code to do this. I have Adobe Acrobat XI Standard, which allows me to edit pdfs manually and create text boxes where needed. Any help would be much appreciated.

Thanks

Community
  • 1
  • 1
chandu
  • 95
  • 1
  • 4
  • 12
  • Adobe pdf editing is generally done through javascript. I suggest going over to adobe's sight and reading the documentation for that. I havent done anything with it extensively, but I do recall learning that when I was curious about this myself. – Doug Coats Nov 04 '16 at 12:26
  • https://acrobatusers.com/tutorials/javascript_console – Doug Coats Nov 04 '16 at 12:32
  • 1
    i have found an answer myself through adobe's api. you can actually extensively code with their OLE automation document and API references – chandu Nov 08 '16 at 15:49

2 Answers2

3

There a several ways to communicate with acrobat from vba/vbs (see Acrobat SDK / IAC section). But for me the best is to work with Acrobat Form API, which allows to work more ore less direct with js-code.

Following you will find an vbs/vba example how to add text on the first page (zero based: first page = 0).

Please look for further methods and properties and especially for defining the rect (place/array to set the box) at the "Acrobat JavaScript API Reference". Good luck, Reinhard

Path = "D:\Test.pdf"

Set App = CreateObject("Acroexch.app")
app.show
Set AVDoc = CreateObject("AcroExch.AVDoc")
Set AForm = CreateObject("AFormAut.App") 'from AFormAPI

If AVDoc.Open(Path,"") Then
    '// write some js code on a vbs variable
   js = "f = this.addField(""newField"", ""text"", 0, [250,650,20,600]);" &vblf _
    & "f.value = ""any Text""; " &vblf _
    & "f.flatten"
     '//execute the js code
    AForm.Fields.ExecuteThisJavaScript js
end if

Set AForm = Nothing
Set AVDoc = Nothing
Set APP = Nothing
ReFran
  • 897
  • 8
  • 14
2

After trawling through adobes documentation on OLE automation and API references, I found that you can actually code very well in VBA with acrobat. Enjoy and adapt to your needs. Here is a link to adobe's API reference: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/iac_api_reference.pdf

Sub RMS_pdf()

 Application.DisplayAlerts = False
 Application.CutCopyMode = False


Set wb1 = Workbooks.Open(Filename:="\\ldnfortunenet\fortunenetdocuments\William Hadman\Suitability\RMS\RMS Product Governance Reports\" & LongNewDate & "\RMS Product Governance Report.xlsm", UpdateLinks:=True)

Set ws1 = wb1.Sheets("Performance & Volatility")
    Set ws2 = wb1.Sheets("Asset Allocation")
    Set ws3 = wb1.Sheets("Detailed Contribution")


OpenPath = "\\ldnfortunenet\fortunenetdocuments\William Hadman\Aushir Shah\RMS Product Governance Reports\RMS Product Governance Report - 2016-10-31 -  CDF Direct Diversified Income.pdf"
SavePath = "\\ldnfortunenet\fortunenetdocuments\William Hadman\Aushir Shah\RMS Product Governance Reports\RMS Product Governance Report - 2016-10-31 -  CDF Direct Diversified Income v2.pdf"
'initialize the Acrobat interface
Set AcroApp = CreateObject("AcroExch.App")
Set gpdDoc = CreateObject("AcroExch.PDDoc")
'open the file
If gpdDoc.Open(OpenPath) Then
Set jso = gpdDoc.GetJSObject
End If
'get at the jso
If Not jso Is Nothing Then
If gpdDoc.Open(dest) Then
Set jso = gpdDoc.GetJSObject
End If

LastPage = gpdDoc.GetNumPages - 1

Set Box1 = jso.AddField("Performance Comment - " & StrategyName(4), "text",            LastPage, Array(583.8, 706.32, 224.7, 583.2))
Box1.TextFont = "Helvetica"
Box1.TextSize = 8

Else
End If
If gpdDoc.Save(PDSaveFull, SavePath) = False Then
MsgBox "Unable to save image"
Else
End If
chandu
  • 95
  • 1
  • 4
  • 12
  • very cool - i hadnt gotten this far (mostly due to lack of trying) – Doug Coats Nov 08 '16 at 17:54
  • Mmmh, I gave you the answer 3 days ago, But you are right jso is also a way to go, – ReFran Nov 08 '16 at 20:28
  • apologies ReFran, I hadn't seen your code. Although it should be worth noting that yours uses AVDoc, while mine uses PDDoc. The differences are: The Acrobat Viewer (AV) layer deals with the viewer’s user interface, whereas The Portable Document (PD) layer provides access to components of PDF documents. The later being perhaps more useful for my needs in this situation. – chandu Nov 10 '16 at 12:01