vba - 如何将特定页面从一个 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)
解决方案
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
推荐阅读
- r - 使用列表元素的名称向列表中的每个数据框添加一列
- flutter - 在按钮内拉伸边框
- openmdao - 在 Openmdao 中使用 ExternalCodeComp 的问题
- mysql - ShellSript 中的 MySql RegexP 正在中断
- java - 如何在一次输入的字符串中计算大写元音和小写元音?
- r - 如何将每行中包含条目的列数相加?
- asp.net - 如何在使用 IIS Express 在 Visual Studio 2015 中进行调试时从 CAC 获取 Request.ServerVariables("CERT_SUBJECT")
- bash - Bash - If 语句执行空比较
- angular9 - 错误:找不到模块:错误:无法解析 ***/node_modules/ng2-pdf-viewer/__ivy_ngcc__/node_modules/pdfjs-dist/build 中的“fs”
- r - 使用 dplyr::group_by() 预测每组黄土回归后的特定 x 值