0

I've ran into a problem - I need to add a custom stamp (type of annotation) to a number of .pdf files. I can do it through Actions for Acrobat X Pro, but my clients do not have that license and they still need to do it. The list of files is stored in Excel spreadsheet, so ideally I am looking for a VBA solution. I have came up with the following code :

Option Explicit
Sub code1()
Dim app As Acrobat.AcroApp
Dim pdDoc As Acrobat.CAcroPDDoc
Dim page As Acrobat.CAcroPDPage
Dim recter(3) As Integer 'Array defining the rectangle of the stamp - in real code wil be calculated, simplified for ease of reading

Dim jso As Object
Dim annot As Object
Dim props As Object
Set pdDoc = Nothing
Set app = CreateObject("AcroExch.App")
Set pdDoc = CreateObject("AcroExch.PDDoc")

recter(0) = 100
recter(1) = 100
recter(2) = 350
recter(3) = 350

pdDoc.Open ("C:\Users\maxim_s\Desktop\Code_1\test1.pdf")

Set jso = pdDoc.GetJSObject

If Not jso Is Nothing Then

Set page = pdDoc.AcquirePage(0)

Set annot = jso.AddAnnot

Set props = annot.getprops
    props.page = 0
    props.Type = "Stamp"
    props.AP = "#eIXuM60ZXCv0sI-vxFqvlD" 'this line throws an error. The string is correct name of the stamp I want to add
    props.rect = recter
annot.setProps props

If pdDoc.Save(PDSaveFull, "C:\Users\maxim_s\Desktop\Code_1\test123.pdf") = False Then
    MsgBox "fail"
    pdDoc.Close
Else
    MsgBox "success"
    pdDoc.Close
End If
End If
End Sub

The problem is with the setprops and getprops procedures - it seems that at the moment when annotation is created (jso.AddAnnot) it does not posses the AP property, which is the name of the stamp I want to add. If I set the property Type= "Stamp" first and then try to specify the AP, the result is that one of the default stamps is added and it's AP is renamed to my custom stamps' AP. Also note, that if I launch acrobat and use the code below, the proper stamp is added:

this.addAnnot({page:0,type:"Stamp",rect:[100,100,350,350],AP:"#eIXuM60ZXCv0sI-vxFqvlD"})

If there is a way to execute this Javascript from VBA inside of the PDDoc object, that will solve the problem, but so far I have failed.

mozgov_net
  • 317
  • 2
  • 12
  • 1
    Hi, I don't know know how to do that too. But how about using VBA to insert a Textbox with your stamp/notation as the content. Print/export the files as PDF and hide/clear the Textbox afterwards? – Doug Oct 18 '16 at 01:19

2 Answers2

2

You can use "ExecuteThisJavaScript" from the AForm Api. Short example:

Set AForm = CreateObject("AFormAut.App")

AForm.Fields.ExecuteThisJavaScript "var x = this.numPages; app.alert(x);"

It has the advantage that you don't need to translate the js examples into jso code. If you search for ExecuteThisJavaScript you will get some more and longer examples.

Good luck, reinhard

ReFran
  • 897
  • 8
  • 14
  • This is great solution! It seems to require AVDoc object to work properly, which is significantly slower than PDDoc operation, but it works! Do you know if there is a way to execute Javascript from a PDDoc object? P.S. Why reinhard? – mozgov_net Oct 19 '16 at 15:53
  • "..Do you know if there is a way to execute Javascript from a PDDoc object?..". I don't know, you may test it, perhaps with .GetPDDOC or open the Doc direct wit the JS-Code. To work faster you may operate in hidden mode. Reinhard is my first name. If I search in google for: "ExecuteThisJavaScript + Reinhard" I get all my examples independent from the Forum where I published (Adobe, VBS, auotIT,..) – ReFran Oct 21 '16 at 12:59
0

In...

props.Type = "Stamp"

The type should be lower case. But if the pure JavaScript is working from the console, you might consider just executing the string using the jso.

joelgeraci
  • 4,606
  • 1
  • 12
  • 19
  • I cant seem to understand how to insert the pure java into the vba code and get java to run. The best I got so far is to actually open the .pdf and display the javascript debugger with needed code in it, but i have to manually press ctrl-enter, which is not a solution. – mozgov_net Oct 18 '16 at 16:06