PDFDocument (arcpy.mapping)
摘要
可对 PDF 文档进行操作,包括合并页面、删除页面、设置文档打开方式、添加文件附件以及创建或更改文档安全性设置。
讨论
PDFDocumentOpen 和 PDFDocumentCreate 是用于对 PDFDocument 对象进行引用的两个函数。
使用 appendPages、insertPages 或 attachFile 方法时,需要传递一个表示现有 PDF 文件路径的字符串。如果传递了 PDFDocument 对象,则该方法将失效。同时,所有要追加、插入或附加的文档必须具有相同的 PDF 安全性。例如,如果要将三个文档追加到一个多页面 PDF 中,并且仅第一个文档具有密码保护,那么在使用 appendPages 之前,必须同样为其余两个文档设置该密码。
在仅替换发生更改的页面时,deletePages 方法会十分有用。处理大量页面会耗费很长时间。如果更改的页面相对较少,那么仅删除这些页面,然后再使用 insertPages 方法插入刚刚更新的页面会比较省时。
有关如何创建地图册的详细说明,请参阅使用 ArcGIS 构建地图册帮助主题。
语法
参数 | 说明 | 数据类型 |
pdf_path |
A string that specifies the path and file name for the resulting PDF file when the saveAndClose method is called. | String |
PDFDocumentCreate 函数接收路径以确定新创建的 PDF 文件的保存位置和文件名。尽管如此,如果不对 PDF 文件执行插入或追加页面以及保存等后续操作,仍不能生成 PDF 文件。PDFDocumentCreate 将返回脚本应执行和保存的 PDFDocument 对象。使用此函数的常见情况是创建 PDF 地图册。这些步骤通常包括从地图文档导出一定数量的独立 PDF 文件、创建新的 PDFDocument 对象、从已导出的 PDF 文件和其他文档追加内容以及保存最终的 PDF 地图册。
请注意,既不可能创建空白 PDF 文件,PDFDocumentCreate 函数也不可能将任何空白页添加到文档内容中。要使 saveAndClose 方法能够成功创建文件,必须使用 appendPages 或 insertPages 方法将内容添加到 PDFDocument 对象中。
有关如何创建地图册的详细说明,请参阅使用 ArcGIS 构建地图册帮助主题。
此脚本将创建新 PDF 文档、追加来自三个独立 PDF 文档的内容并保存生成的 PDF 文件。
import arcpy, os
#Set file name and remove if it already exists
pdfPath = r"C:\Project\ParcelAtlasMapBook.pdf"
if os.path.exists(pdfPath):
os.remove(pdfPath)
#Create the file and append pages
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfPath)
pdfDoc.appendPages(r"C:\Project\Title.pdf")
pdfDoc.appendPages(r"C:\Project\ParcelAtlas.pdf")
pdfDoc.appendPages(r"C:\Project\ContactInfo.pdf")
#Commit changes and delete variable reference
pdfDoc.saveAndClose()
del pdfDoc
属性
属性 | 说明 | 数据类型 |
pageCount (只读) |
Returns an integer that represents the total number of pages in the PDF document | Long |
方法概述
方法 | 说明 |
appendPages (pdf_path, {input_pdf_password}) |
Appends one PDF document to the end of another |
attachFile (file_path, {description}) |
Attaches files to existing PDF documents (Attachments are then accessible to users when the PDF file is opened in a PDF viewer application.) |
deletePages (page_range) |
用于删除现有 PDF 文档内一个或多个页面。 |
insertPages (pdf_path, {before_page_number}, {input_pdf_password}) |
Allows inserting the contents of one PDF document at the beginning or in between the pages of another PDFDocument |
saveAndClose () |
Saves any changes made to the currently referenced PDFDocument |
updateDocProperties ({pdf_title}, {pdf_author}, {pdf_subject}, {pdf_keywords}, {pdf_open_view}, {pdf_layout}) |
Allows updating of the PDF document metadata and can also set the certain behaviors that will trigger when the document is opened in Adobe Reader or Adobe Acrobat, such as the initial view mode and the page thumbnails view |
updateDocSecurity (new_master_password, {new_user_password}, {encryption}, {permissions}) |
提供用于对 PDF 文件设置密码、加密和安全性限制的机制。 |
方法
参数 | 说明 | 数据类型 |
pdf_path |
A string that includes the location and name of the input PDF document to be appended | String |
input_pdf_password |
A string that defines the master password to a protected file (默认值为 None) | String |
When appending secured PDF documents that each have different security settings, the output settings will be based on the primary document that pages are being appended to. For example, if the document that is being appended to does not have password information saved, but the appended pages do, the resulting document will not have password information saved.
参数 | 说明 | 数据类型 |
file_path |
A string that includes the location and name of the file to be attached to the PDF document. | String |
description |
An optional string to be used as a description for the attachment. The user will see this string when viewing the attachment in a PDF viewer application. | String |
Use the attachFile method to attach any type of file to a PDF document.
参数 | 说明 | 数据类型 |
page_range |
A string that defines the page or pages to be deleted. Delete a single page by passing in a single value as a string (for example, "3"). Multiple pages can be deleted using a comma between each value (for example, "3, 5, 7"). Ranges can also be applied (for example, "1, 3, 5-12" ). | String |
因为每次删除页面后,内置的 PDF 页码会自动调整,所以跟踪删除的页面十分重要。例如,把第 1 页或第 2 页删除后,第 3 页将立即成为第 2 页。如果第 1 页和第 2 页被删除,则第 3 页成为第 1 页。如果您正在使用 deletePages,然后立即使用 appendPages 或 insertPages 则需要考虑这一点。
参数 | 说明 | 数据类型 |
pdf_path |
A string that includes the location and name of the input PDF document to be inserted. | String |
before_page_number |
An integer that defines a page number in the currently referenced PDFDocument before which the new page(s) will be inserted. For example, if the before_page_value is 1, the inserted page will be inserted before all pages. (默认值为 1) | String |
input_pdf_password |
A string that defines the master password to a protected file. (默认值为 None) | String |
To add pages to the end of the current PDFDocument, use appendPages instead.
When inserting secured PDF documents that have different security settings, the output settings will be based on the primary document that pages are being inserted into. For example, if the document that is being inserted into does not have password information saved, but the inserted pages do, the resulting document will not have password information saved.
The saveAndClose method must be used for changes to be maintained. If a script exits before saveAndClose is executed, changes will not be saved.
参数 | 说明 | 数据类型 |
pdf_title |
A string defining the document title, a PDF metadata property. (默认值为 None) | String |
pdf_author |
A string defining the document author, a PDF metadata property. (默认值为 None) | String |
pdf_subject |
A string defining the document subject, a PDF metadata property. (默认值为 None) | String |
pdf_keywords |
A string defining the document keywords, a PDF metadata property. (默认值为 None) | String |
pdf_open_view |
A string or number that will define the behavior to trigger when the PDF file is viewed. The default value is USETHUMBS, which will show the Adobe Reader Pages panel automatically when the PDF is opened.
(默认值为 USE_THUMBS) | String |
pdf_layout |
A string or number that will define the initial view mode to trigger when the PDF file is viewed.
(默认值为 SINGLE_PAGE) | String |
A pdf_open setting of FULL_SCREEN will prompt a warning about full-screen mode when the PDF is opened. Setting pdf_open to a different option will not clear this setting unless pdf_open is set to USE_NONE.
参数 | 说明 | 数据类型 |
new_master_password |
A string that defines the master document password. This password is required for appending and inserting pages into a secured PDF. | String |
new_user_password |
A string that defines the user password needed to open the PDF document for viewing. (默认值为 None) | String |
encryption |
A string that defines the encryption technique used on the PDF.
(默认值为 RC4) | String |
permissions |
A string that defines the capabilities restricted by the document security settings. The permissions argument can accept a list of strings describing all the options to be restricted. The document restrictions can be viewed in Adobe Acrobat in the Document Properties Document Restrictions Summary page.
(默认值为 ALL) | String |
可通过将 new_master_password 和 new_user_password 属性设置为空字符串,以便轻松删除受保护 PDF 文档的密码。
代码实例
此脚本将创建新的 PDF 文档、追加两个独立 PDF 文档中的内容并保存生成的 PDF 文件。
import arcpy, os
#Set file name and remove if it already exists
pdfPath = r"C:\Project\ParcelAtlasMapBook.pdf"
if os.path.exists(pdfPath):
os.remove(pdfPath)
#Create the file and append pages
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfPath)
pdfDoc.appendPages(r"C:\Project\Title.pdf")
pdfDoc.appendPages(r"C:\Project\ParcelAtlas.pdf")
#Commit changes and delete variable reference
pdfDoc.saveAndClose()
del pdfDoc
以下脚本修改了示例 1 中所创建的 PDF 文档的元数据属性,并对文档的打开样式进行了设置。
import arcpy
pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf")
pdfDoc.updateDocProperties(pdf_title="Atlas Map",
pdf_author="Author",
pdf_subject="Map Book",
pdf_keywords="Atlas; Map Books",
pdf_open_view="USE_THUMBS",
pdf_layout="SINGLE_PAGE")
pdfDoc.saveAndClose()
del pdfDoc
以下脚本将用户密码设置为 esri,使用 RC4 压缩方法对 PDF 进行加密并且在打开文档时需要提供密码。
import arcpy
pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf")
pdfDoc.updateDocSecurity("master", "user", "RC4", "OPEN")
pdfDoc.saveAndClose()
del pdfDoc
以下脚本将在现有多页文档的结尾追加最后一页内容。对受保护的 PDF 文档进行这种更改时需要提供主密码。
import arcpy
pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf", "master")
pdfDoc.appendPages(r"C:\Project\ContactInfo.pdf")
pdfDoc.saveAndClose()
del pdfDoc
以下脚本将使用 deletePages 然后使用 insertPages 替换现有 PDF 中的四页内容。请注意在现在显示为第 3 页的页面前插入新第 3 页的方法,在最初的第 3 页移除之前,现在的第 3 页其实是第 4 页。在 5-7 页中同样应用了该方法。
import arcpy
pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf", "master")
pdfDoc.deletePages("3, 5-7")
pdfDoc.insertPages(r"C:\Project\NewPage3.pdf", 3, "master")
pdfDoc.insertPages(r"C:\Project\NewPages5-7.pdf", 5, "master")
pdfDoc.saveAndClose()
del pdfDoc