我遇到了一个问题 - 我需要向多个 .pdf 文件添加自定义图章(注释类型)。我可以通过 Actions for Acrobat X Pro 来完成,但我的客户没有该许可证,他们仍然需要这样做。文件列表存储在 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
andgetprops
过程 - 似乎在创建注释 ( 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,那将解决问题,但到目前为止我失败了。