我正在尝试从Excel vba中执行以下操作:
我已经想出了第一步。对于第二步和第三步,我可以使用Adobe Acrobat XI Pro。由于我想从vba一次性完成此操作,因此我下载了Acrobat SDK。从一些快速的谷歌搜索,我想我现在应该能够使用IAC找出第二步,但第三步(奇怪的)似乎是最困难的。任何建议都会受到欢迎。
最佳, NIH
答案 0 :(得分:2)
与此同时,我找到了一个添加页码的解决方案。对于任何可能感兴趣的人,这里有一个如何做到的例子:
Sub addPageNumbers()
Dim acroApp As Acrobat.acroApp
Dim myDocument As Acrobat.AcroPDDoc
Dim jso As Object
Dim strPath As String
Dim strFileName As String
Dim intPages As Integer
Dim i As Integer
Set acroApp = CreateObject("AcroExch.App")
Set myDocument = CreateObject("AcroExch.PDDOc")
strPath = "C:\"
strFileName = "myDoc.pdf"
'Open file and load JSObject
Set myDocument = CreateObject("AcroExch.PDDOc")
myDocument.Open (strPath & strFileName)
Set jso = myDocument.GetJSObject
' get number of pages
intPages = myDocument.GetNumPages
'Write page numbers to all pages
For i = 1 To intPages
jso.addWatermarkFromText _
cText:=Str(i) & " ", _
nTextAlign:=1, _
nHorizAlign:=2, _
nVertAlign:=4, _
nStart:=i - 1, _
nEnd:=i - 1
Next i
'Save document
Call myDocument.Save(1, strPath & strFileName)
'Clean up
Set jso = Nothing
Call acroApp.CloseAllDocs
Set myDocument = Nothing
Call acroApp.Exit
Set acroApp = Nothing
End Sub
请记住,您需要在计算机上安装Acrobat(不仅仅是阅读器),并且必须在vba编辑器中启用对Acrobat的引用。
我没有添加错误处理;显然你应该。
可以找到有关addwatermarkFromText方法的更多信息here
致以最诚挚的问候,
NIH
答案 1 :(得分:1)
这是另一种方法。我使用acrobat js中的add field方法。 " ExecuteThisJavaScript"方法的优点是你可以使用js而无需转换为js-object。
以下示例 - 我已经在某处发布了 - 将日期,文件名和pageNo作为页脚添加到pdf中。它是用VBS编写的,但也可以在没有更改的情况下用作vba。
最好的问候,Reinhard
File = "D:\Test.pdf"
Set App = CreateObject("Acroexch.app") '//start acrobat
app.show '//show Acrobat or comment out for hidden mode
Set AVDoc = CreateObject("AcroExch.AVDoc")
Set AForm = CreateObject("AFormAut.App") '//get AFormAPI to execute js later
If AVDoc.Open(File,"") Then
'//write JS-Code on a variable
Ex = " // set Date, filename and PageNo as footer "&vbLF _
& " var Box2Width = 50 "&vbLF _
& " for (var p = 0; p < this.numPages; p++) "&vbLF _
& " { "&vbLF _
& " var aRect = this.getPageBox(""Crop"",p); "&vbLF _
& " var TotWidth = aRect[2] - aRect[0] "&vbLF _
& " { var bStart=(TotWidth/2)-(Box2Width/2) "&vbLF _
& " var bEnd=((TotWidth/2)+(Box2Width/2)) "&vbLF _
& " var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]); "&vbLF _
& " fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages; "&vbLF _
& " fp.textSize=6; fp.readonly = true; "&vbLF _
& " fp.alignment=""center""; "&vbLF _
& " } "&vbLF _
& " } "
'//Execute JS-Code
AForm.Fields.ExecuteThisJavaScript Ex
msgBox("Done")
end if
Set AVDoc = Nothing
Set APP = Nothing