PDFDocument
摘要
Allows manipulation of PDF documents, including facilities for merging pages, setting document open behavior, adding file attachments, and creating or changing document security settings
讨论
PDFDocumentOpen and PDFDocumentCreate are two functions that provide a reference to a PDFDocument object.
When using the appendPages, insertPages, or attacheFile methods, it is important to pass a path string to an existing PDF file. If a PDFDocument object is passed, the method will fail. It is also required that PDF security be the same for all documents that are being appended, inserted, or attached. For example, if you want to append three documents into one multipage PDF and only the first document is protected with a password, the other two documents must also first be given the same password before appendPages is used.
For more discussion on how to create map books, see the Building Map Books with ArcGIS help topic.
语法
参数 | 说明 | 数据类型 |
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.) |
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}) |
Provides the mechanism that sets password, encryption, and security restrictions on PDF files. |
方法
参数 | 说明 | 数据类型 |
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.
参数 | 说明 | 数据类型 |
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 |
A password on a secured PDF document can be removed simply by setting the new_master_password and new_user_password properties to empty strings.
代码示例
This script will create a new PDF document, append the contents of two separate PDF documents, and save the resulting PDF file.
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
The following script modifies the PDF document metadata properties for the document created in example 1 above and sets the style in which the document will open.
import arcpy pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf") pdfDoc.updateDocProperties(pdf_title="Atlas Map", pdf_author="ESRI", pdf_subject="Map Book", pdf_keywords="Atlas; Map Books", pdf_open_view="USE_THUMBS", pdf_layout="SINGLE_PAGE") pdfDoc.saveAndClose() del pdfDoc
The following script will set the user password to esri, encrypt the PDF using RC4 compression, and require a password when the document is opened.
import arcpy pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf") pdfDoc.updateDocSecurity("master", "user", "RC4", "OPEN") pdfDoc.saveAndClose() del pdfDoc
This last example will append a final page to the end of the already existing multipage document. The master password is required to make this change to a secured PDF document.
import arcpy pdfDoc = arcpy.mapping.PDFDocumentOpen(r"C:\Project\ParcelAtlasMapBook.pdf", "master") pdfDoc.appendPages(r"C:\Project\ContactInfo.pdf") pdfDoc.saveAndClose() del pdfDoc