首页 > 解决方案 > 如何将特定页面从一个 pdf 附加到另一个 pdf?

问题描述

目前我有将pdf组合在一起的代码。它从我在 A3:A5 列中指定的每个文件中获取所有页面并附加到 A2。

假设我所有的 pdf 文件各有 5 页。但是,如果我只想取前 3 张 A3、整整 5 页 A4 和 1 页 A5,该怎么办?

我也不需要在页面之间指定,即 A3 的 2 、 4 和 5 。它将始终按顺序排列,即 1-3 或 1-5 或 1-2。

我有一个已经获取页数的计数器

  Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber

完整代码:

Sub main3()

    Set app = CreateObject("Acroexch.app")

    Dim FilePaths As Collection
    Set FilePaths = New Collection
    Dim AcroDoc As Object
    Set AcroDoc = New AcroPDDoc

    'Counts # of pages in each pdf, loads to column D.

    Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber


    'Append to this file, ideally will be a front page to append to, commented out for now.

    'FilePaths.Add "\path\name\here"

    'Active or not feature in Column B, Specify Yes to include in combination, no to exclude

    Dim cell As Range
    For Each cell In Range("A2:A100")
    If cell.Offset(0, 1).Value2 <> "No" Then FilePaths.Add cell.Value2
    Next cell


    'Combine files which are listed in Column A.

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

    For colIndex = 2 To FilePaths.Count
        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

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        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
    MsgBox "DONE"
End Sub

任何有关如何实现这一目标的帮助将不胜感激。

尝试了以下代码,但没有任何效果:

'attempt to do start and end page in col E and F.

    startPage = Range("E" & colIndex)
    endPage = Range("F" & colIndex)
    OK = sourceDoc.DeletePages(1, startPage - 1)
    OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages)

标签: vbaexcel

解决方案


You can try deleting the unwanted parts of each pdf prior to appending them all together with sourceDoc.DeletePages(startPage, endPage) for example:

OK = sourceDoc.Open(FilePaths(colIndex))

startPage = Range("C" & colIndex)
endPage = Range("D" & colIndex)
OK = sourceDoc.DeletePages(1, startPage - 1)
OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages) ' just some arithmetic

Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

You would just need to specify startPage and endPage for each in columns C & D... or you can change this snippet and specificy them however you prefer


推荐阅读