appendPages
汇总
可对 PDF 文档进行管理,包括合并和删除页面、设置文档打开方式以及创建或更改文档安全性设置。
讨论
PDFDocumentOpen 和 PDFDocumentCreate 是用于对 PDFDocument 对象进行引用的两个函数。
创建新 PDF 文件的一个常见情况是用于创建地图册。一般步骤包括创建新 PDFDocument 对象、从现有 PDF 文件追加内容以及将 PDFDocument 对象保存到磁盘。另一个最常见情形是修改现有 PDF 文件内容或属性。一旦引用了 PDFDocument ,可执行 appendPages 、 insertPages 、 deletePages ,还可使用 updateDocProperties 和 updateDocSecurity 方法修改 PDF 文件设置。
在仅替换发生更改的页面时, deletePages 方法会十分有用。处理大量页面会耗费很长时间。如果更改的页面相对较少,那么仅删除这些页面,然后再使用 insertPages 方法插入刚刚更新的页面会比较省时。
目前,使用 Python 设置文档的 PDF 安全性时,将限定为 RC4 加密。如果在 ArcGIS Pro 中设置 PDF 安全性,则将限定为 AES 256-bit 加密。这意味着如果您使用 Python 管理 PDF 文档,则将被限定为只能处理不具安全性或仅使用 RC4 加密的 PDF 文档。
语法
appendPages()
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
pageCount (只读) |
返回一个整型,该整型表示 PDF 文档的总页数。 |
Long |
方法
appendPages(pdf_path, {input_pdf_password})
将一个 PDF 文档附加在另一文档结尾。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
pdf_path |
包含要附加的输入 PDF 文档位置和名称的字符串。 |
String |
|
input_pdf_password (可选) |
用于定义受保护文件的主密码的字符串。必须为主密码;用户密码将不起作用。 (默认值为 None) |
String |
deletePages(page_range)
可用于删除现有 PDF 文档内的一个或多个页面。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
page_range |
用于定义要删除的一个或多个页面的字符串。通过将单个值以字符串的形式进行传递,删除单个页面(例如, "3" )。可在每个值之间使用逗号来删除多个页面(例如, "3, 5, 7" )。还可以应用范围(例如, "1, 3, 5-12" )。 |
String |
insertPages(pdf_path, {before_page_number}, {input_pdf_password})
可将一个 PDF 文档的内容插入到另一 PDF 文档开头处或页面之间。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
pdf_path |
包含要插入的输入 PDF 文档位置和名称的字符串。 |
String |
|
before_page_number (可选) |
用于在当前引用的 (默认值为 1) |
Integer |
|
input_pdf_password (可选) |
用于定义受保护文件的主密码的字符串。必须为主密码;用户密码将不起作用。 (默认值为 None) |
String |
saveAndClose()
保存对当前参考的 PDFDocument 所做的任何更改。
updateDocProperties({pdf_title}, {pdf_author}, {pdf_subject}, {pdf_keywords}, {pdf_open_view}, {pdf_layout})
更新 PDF 元数据。 您也可以使用此方法设置文档在 Adobe Reader 或 Adobe Acrobat 中打开时将触发的行为,例如初始视图模式和页面缩略图视图。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
pdf_title (可选) |
The document title. This is a PDF metadata property. (默认值为 None) |
String |
|
pdf_author (可选) |
The document author. This is a PDF metadata property. (默认值为 None) |
String |
|
pdf_subject (可选) |
The document subject. This is a PDF metadata property. (默认值为 None) |
String |
|
pdf_keywords (可选) |
The document keywords. This is a PDF metadata property. (默认值为 None) |
String |
|
pdf_open_view (可选) |
Specifies the Adobe Reader view mode that will be used.
(默认值为 USE_THUMBS) |
String |
|
pdf_layout (可选) |
Specifies the Adobe Reader layout mode that will be used.
(默认值为 SINGLE_PAGE) |
String |
updateDocSecurity(new_master_password, {new_user_password}, {encryption}, {permissions[permissions,...]})
为 PDF 设置密码、加密和安全性限制。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
new_master_password |
The primary document password. |
String |
|
new_user_password (可选) |
The user password needed to open the PDF for viewing. (默认值为 None) |
String |
|
encryption (可选) |
Specifies the encryption technique that will be used for the PDF.
(默认值为 AES-256) |
String |
|
permissions[permissions,...] (可选) |
A string or list of strings that specifies the permissions that will be granted by the document security settings.
(默认值为 ALL) |
String |
代码示例
此脚本将创建新 PDF 文档、追加三个独立 PDF 文档的内容并保存生成的 PDF 文件。
import arcpy, os
#Set file name and remove if it already exists
pdfPath = r"C:\Projects\YosemiteNP\AttractionsMapBook.pdf"
if os.path.exists(pdfPath):
os.remove(pdfPath)
#Create the file and append pages
pdfDoc = arcpy.mp.PDFDocumentCreate(pdfPath)
pdfDoc.appendPages(r"C:\Projects\YosemiteNP\Title.pdf")
pdfDoc.appendPages(r"C:\Projects\YosemiteNP\MapPages.pdf")
pdfDoc.appendPages(r"C:\Projects\YosemiteNP\ContactInfo.pdf")
#Commit changes and delete variable reference
pdfDoc.saveAndClose()
del pdfDoc
以下脚本修改 PDF 文档元数据属性并且设置文档以何种样式打开。
import arcpy
pdfDoc = arcpy.mp.PDFDocumentOpen(r"C:\Projects\YosemiteNP\AttractionsMapBook.pdf")
pdfDoc.updateDocProperties(pdf_title="Yosemite Main Attrations Map Book",
pdf_author="Esri",
pdf_subject="Main Attractions Map Book",
pdf_keywords="Yosemite; Map Books; Attractions",
pdf_open_view="USE_THUMBS",
pdf_layout="SINGLE_PAGE")
pdfDoc.saveAndClose()
del pdfDoc
以下脚本将设置 user_password 和 master_password ,使用 RC4 压缩方法对 PDF 进行加密并且在打开文档时需要提供密码。确保已阅读上述类描述中的受保护 PDF 限制。
import arcpy
pdfDoc = arcpy.mp.PDFDocumentOpen(r"C:\Projects\YosemiteNP\AttractionsMapBook.pdf")
pdfDoc.updateDocSecurity("master", "user", "RC4")
pdfDoc.saveAndClose()
del pdfDoc
以下脚本将使用 deletePages 然后使用 insertPages 替换现有 PDF 中的四页内容。请注意在当前显示的第 3 页的页面前插入新第 3 页的方法,在最初的第 3 页移除之前,现在的第 3 页其实是第 4 页。在 5-7 页中同样应用了该方法。确保已阅读上述类描述中的受保护 PDF 限制。
import arcpy
pdfDoc = arcpy.mp.PDFDocumentOpen(r"C:\Projects\YosemiteNP\AttractionsMapBook.pdf", "master")
pdfDoc.deletePages("3, 5-7")
pdfDoc.insertPages(r"C:\Projects\Yosemite\NewPage3.pdf", 3, "master")
pdfDoc.insertPages(r"C:\Projects\Yosemite\NewPages5-7.pdf", 5, "master")
pdfDoc.saveAndClose()
del pdfDoc