VBA, Acrobat Pro Добавить верхний и нижний колонтитулы в PDF - PullRequest
0 голосов
/ 02 декабря 2018

Я использую Acrobat XI Pro с VBA для объединения моих PDF-файлов.

У меня есть код, который добавляет PDF-страницы вместе с помощью Acrobat API, найденный здесь: https://wwwimages2.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/iac_api_reference.pdf

Однако я пытаюсьдля автоматической нумерации страниц или добавления пользовательских сохраненных настроек верхнего и нижнего колонтитула и применения ко всем страницам.

enter image description here enter image description here enter image description here

Вот мой код:

   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

Может кто-нибудь помочь с этим?

1 Ответ

0 голосов
/ 12 декабря 2018

Полный кредит достается @NiH за его сообщение на SO Добавление номеров страниц в pdf через VBA и 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...