【问题标题】:Adding page numbers to pdf through VBA and Acrobat IAC通过 VBA 和 Acrobat IAC 将页码添加到 pdf
【发布时间】:2015-05-19 01:51:24
【问题描述】:

我正在尝试从 Excel vba 中执行以下操作:

  1. 将某些工作表导出为 pdf
  2. 取一个现有的pdf文档,插入到新生成的pdf中的特定位置(不一定是末尾或开头)
  3. 对合并后的 pdf 的页数进行编号,省略两个标题页

我已经想出了第一步。对于第二步和第三步,我可以使用 Adob​​e Acrobat XI Pro。由于我想从 vba 一次性完成此操作,因此我下载了 Acrobat SDK。通过一些快速的谷歌搜索,我想我现在应该能够使用 IAC 找出第二步,但第三步(奇怪的是)似乎是最困难的。欢迎提出任何建议。

最好, 镍氢

【问题讨论】:

    标签: vba pdf acrobat-sdk


    【解决方案1】:

    与此同时,我找到了添加页码的解决方案。对于任何可能感兴趣的人,这里有一个如何完成的示例:

    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

    最好的问候,

    镍氢

    【讨论】:

      【解决方案2】:

      这里有另一种方法。我使用来自 acrobat js 的 add field 方法。 “ExecuteThisJavaScript”方法的好处是不用翻译成js-object就可以使用js。

      以下示例 - 我已经在某处发布 - 添加日期、文件名和 pageNo 作为页脚到 pdf。它是用 VBS 编写的,但也可以用作 vba 而无需更改。

      最好的问候,莱因哈德

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        相关资源
        最近更新 更多