我遇到了一个问题 - 我需要向多个 .pdf 文件添加自定义图章(注释类型)。我可以通过 Acrobat X Pro 的 Actions 来完成此操作,但我的客户没有该许可证,但他们仍然需要这样做。文件列表存储在 Excel 电子表格中,因此理想情况下我正在寻找 VBA 解决方案。我想出了以下代码:
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
问题在于 setprops
和 getprops
过程 - 似乎在创建注释时(jso.AddAnnot
)不具有 AP
属性,这是我要添加的图章的名称。如果我先设置属性 Type= "Stamp"
,然后尝试指定 AP
,结果是添加一个默认标记,它是 AP
重命名为我的自定义图章的 AP
。另请注意,如果我启动 acrobat 并使用下面的代码,则会添加正确的标记:
this.addAnnot({page:0,type:"Stamp",rect:[100,100,350,350],AP:"#eIXuM60ZXCv0sI-vxFqvlD"})
如果有一种方法可以从 PDDoc 对象内部的 VBA 执行此 Javascript,那就可以解决问题,但到目前为止我还没有成功。
最佳答案
您可以使用 AForm Api 中的“ExecuteThisJavaScript”。简短示例:
设置 AForm = CreateObject("AFormAut.App")
AForm.Fields.ExecuteThisJavaScript "var x = this.numPages; app.alert(x);"
它的优点是不需要将js示例翻译成jso代码。如果您搜索 ExecuteThisJavaScript,您将得到一些更多、更长的示例。
祝你好运,莱因哈德
关于javascript - 从 VBA 添加自定义 pdf 图章到文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096240/