通过VBA和Acrobat IAC将页码添加到pdf [英] Adding page numbers to pdf through VBA and Acrobat IAC
问题描述
我正在尝试从Excel vba中执行以下操作:
I am trying to do the following thing from Excel vba:
- 将某些工作表导出为pdf
- 获取现有的pdf文档,并将其插入到新生成的pdf中的特定位置(不一定在末尾或开头)
- 编号合并的pdf的页数,省略两个标题页
我已经弄清楚了第一步.对于第二步和第三步,我可以使用Adobe Acrobat XI Pro.由于我想通过vba一次完成此操作,因此我下载了Acrobat SDK.通过快速谷歌搜索,我认为我现在应该能够使用IAC找出第二步,但是第三步(奇怪的是)似乎是最困难的.任何建议都将受到欢迎.
I already figured out the first step. For the second and third step, I have Adobe Acrobat XI Pro at my disposal. Since I want to do this in one go from vba, I have downloaded the Acrobat SDK. From some quick Googling, I think I should be able to figure out the second step now, using the IAC, but the third step (oddly) seems the most difficult. Any suggestions would be welcome.
最好, 镍氢
推荐答案
同时,我找到了添加页码的解决方案.对于任何可能感兴趣的人,下面是如何完成此操作的示例:
In the meantime, I found a solution for adding page numbers. For anyone who might be interested, here's an example of how it can be done:
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的引用.
Keep in mind that you need to have Acrobat (not only the reader) installed on your computer, and the reference to Acrobat has to be enabled in the vba editor.
我没有添加错误处理;显然你应该.
I did not add error handling; obviously you should.
有关addwatermarkFromText方法的更多信息,请参见
More info on the addwatermarkFromText method can be found here
最诚挚的问候,
镍氢
这篇关于通过VBA和Acrobat IAC将页码添加到pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!