【问题标题】:VBA, Acrobat Pro Add Header&Footer to PdfVBA、Acrobat Pro 添加页眉和页脚到 Pdf
【发布时间】:2018-12-02 15:27:18
【问题描述】:

我使用 acrobat xi pro 和 vba 来合并我的 pdf 文件。

我有一个代码,它使用此处找到的 acrobat api 将 pdf 页面附加在一起: https://wwwimages2.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/iac_api_reference.pdf

但是我试图自动为页面编号,或者添加我自定义保存的页眉和页脚设置并应用于所有页面。

这是我的代码:

   Dim acroExchangeApp As Object
    Set app = CreateObject("Acroexch.app")

    Dim filePaths As Collection     'Paths for PDFS to append
    Set filePaths = New Collection
    Dim fileRows As Collection      'Row numbers PDFs to append
    Set fileRows = New Collection
    Dim sourceDoc As Object
    Dim primaryDoc As Object        ' PrimaryDoc is what we append too
    Dim insertPoint As Long         ' PDFs will be appended after this page in the primary Doc
    Dim startPage As Long           ' First desired page of appended PDF
    Dim endPage As Long             ' Last desired page of appended PDF
    Dim colIndex As Long            '
    Dim numPages As Long
    Dim acroDoc As Object
    Set acroDoc = New AcroPDDoc


    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To filePaths.count
        query_start_time = time()
        start_memory = GetWorkingMemoryUsage

        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK


     numberOfPagesToInsert = sourceDoc.GetNumPages

        'inserts pages
        acroDoc.Open source_file_name

        insertPoint = acroDoc.GetNumPages - 1


        If endPage > 1 Then
            OK = primaryDoc.InsertPages(insertPoint, sourceDoc, startPage, endPage - startPage, False)
            Debug.Print "(" & colIndex & ") " & endPage - startPage & " PAGES INSERTED SUCCESSFULLY: " & OK
        Else
            OK = primaryDoc.InsertPages(insertPoint, sourceDoc, startPage, endPage - startPage + 1, False)
            Debug.Print "(" & colIndex & ") " & endPage - startPage + 1 & " PAGES INSERTED SUCCESSFULLY: " & OK
        End If


           Set sourceDoc = Nothing

    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing

有人可以帮忙吗?

【问题讨论】:

  • 问题是什么?
  • @HackSlash 抱歉,您能帮我在每个 pdf 中自动插入页脚吗?
  • 通过一些快速的在线研究,Acrobat 的 API 中似乎没有一种方法可以做到这一点。但是,如果您想看看是否有帮助,似乎确实有 possible workaround
  • @K.Dᴀᴠɪs 谢谢你,我会调查的。

标签: excel vba


【解决方案1】:

感谢@NiH 在 SO Adding page numbers to pdf through VBA and Acrobat IAC 上的帖子

我在下面修改了您的代码以包含他使用 JavaScript 对象:

内部修改:

'********************************************** *************** '****************************************************** ************

Dim acroExchangeApp As Object
    Set app = CreateObject("Acroexch.app")

    Dim filePaths As Collection     'Paths for PDFS to append
    Set filePaths = New Collection
    Dim fileRows As Collection      'Row numbers PDFs to append
    Set fileRows = New Collection
    Dim sourceDoc As Object
    Dim primaryDoc As Object        ' PrimaryDoc is what we append too
    Dim insertPoint As Long         ' PDFs will be appended after this page in the primary Doc
    Dim startPage As Long           ' First desired page of appended PDF
    Dim endPage As Long             ' Last desired page of appended PDF
    Dim colIndex As Long            '
    Dim numPages As Long
    Dim acroDoc As Object
    Set acroDoc = New AcroPDDoc


    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To filePaths.count
        query_start_time = time()
        start_memory = GetWorkingMemoryUsage

        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK


     numberOfPagesToInsert = sourceDoc.GetNumPages

        'inserts pages
        acroDoc.Open source_file_name

        insertPoint = acroDoc.GetNumPages - 1

        If endPage > 1 Then
            OK = primaryDoc.InsertPages(insertPoint, sourceDoc, startPage, endPage - startPage, False)
            Debug.Print "(" & colIndex & ") " & endPage - startPage & " PAGES INSERTED SUCCESSFULLY: " & OK
        Else
            OK = primaryDoc.InsertPages(insertPoint, sourceDoc, startPage, endPage - startPage + 1, False)
            Debug.Print "(" & colIndex & ") " & endPage - startPage + 1 & " PAGES INSERTED SUCCESSFULLY: " & OK
        End If

           Set sourceDoc = Nothing

    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))

        '*************************************************************
        '*************************************************************
        Dim jso As Object

        Set jso = primaryDoc.GetJSObject


        'Write page numbers to all pages
        For i = 1 To primaryDoc.GetNumPages
            jso.addWatermarkFromText _
                cText:=Str(i) & "  ", _
                nTextAlign:=1, _
                nHorizAlign:=2, _
                nVertAlign:=4, _
                nStart:=i - 1, _
                nEnd:=i - 1
        Next i
        '*************************************************************
        '*************************************************************

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing

【讨论】:

  • 嗨,你能解释一下*的吗?您是说这是我需要添加到代码中的部分吗?
  • 是的,'****** 中的所有内容。虽然你应该能够用我上面的代码替换你的代码......
  • 嘿,谢谢,但由于某种原因,它只编号前 3 页。你知道为什么会这样吗?同样运行这个新添加的代码,我的 pdf 文件大小从 10k KB 到 20k KB,3 页码!
  • 我尝试用getnumpages 替换numpagestoinsert 这可行,但我的pdf 文件超过300k Kb,最后仍然缺少2 页。
  • 我已经修改了我的原始答案并将“页码插入”代码移动到原始“保存”命令下方(并在移动代码之后重复保存命令)。这应该允许访问新的、增加的页数。关于文件大小的增加,您偶然发现了一个常见问题。正在嵌入字体,这导致了增加。您也在使用 PDSaveFull。有一个名为 AVDocSaveOptimized 的函数。我会看看可以做些什么来利用它。
【解决方案2】:

我试过 AddField 和水印。

AddField 需要

For i = 0 To intPages - 1
    Set objTextfeld = jso.AddField("Textfeld" & i, "text", i, Array(250, 50, 300, 0))
    objTextfeld.Value = "--" & Str(i + 1) & " --"
    objTextfeld.textSize = 10
    objTextfeld.textFont = "Calibri"
Next i

完整代码如下:

Sub addPageNumbers(sFile As String)
Dim AcroApp As Acrobat.CAcroApp
Dim jso As Object
Dim KurzGesamt As Acrobat.CAcroPDDoc
Dim i As Integer, intPages As Integer
Dim objTextfeld As Object

Set AcroApp = CreateObject("AcroExch.App")
Set KurzGesamt = CreateObject("AcroExch.PDDoc")
KurzGesamt.Open (sFile)
Set jso = KurzGesamt.GetJSObject
intPages = KurzGesamt.GetNumPages

For i = 0 To intPages - 1
    Set objTextfeld = jso.AddField("Textfeld" & i, "text", i, Array(250, 50, 300, 0))
    objTextfeld.Value = "--" & Str(i + 1) & " --"
    objTextfeld.textSize = 10
    objTextfeld.textFont = "Calibri"
Next i

jso.FlattenPages

Call KurzGesamt.Save(1, sFile)

Set jso = Nothing
Call AcroApp.CloseAllDocs
Set KurzGesamt = Nothing
Call AcroApp.Exit
Set AcroApp = Nothing
'Debug.Print "Done!"
End Sub

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 2018-08-17
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2013-09-30
    • 2023-03-12
    相关资源
    最近更新 更多