PDFDocument

摘要

可对 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 文档。

属性

属性说明数据类型
pageCount
(只读)

返回一个整型,该整型表示 PDF 文档的总页数。

Long

方法概述

方法说明
appendPages (pdf_path, {input_pdf_password})

将一个 PDF 文档附加在另一文档结尾。

deletePages (page_range)

可用于删除现有 PDF 文档内的一个或多个页面。

insertPages (pdf_path, {before_page_number}, {input_pdf_password})

可将一个 PDF 文档的内容插入到另一 PDF 文档开头处或页面之间。

saveAndClose ()

保存对当前参考的 PDFDocument 所做的任何更改。

updateDocProperties ({pdf_title}, {pdf_author}, {pdf_subject}, {pdf_keywords}, {pdf_open_view}, {pdf_layout})

更新 PDF 元数据。 您也可以使用此方法设置文档在 Adobe Reader 或 Adobe Acrobat 中打开时将触发的行为,例如初始视图模式和页面缩略图视图。

updateDocSecurity (new_master_password, {new_user_password}, {encryption}, {permissions})

为 PDF 设置密码、加密和安全性限制。

方法

appendPages (pdf_path, {input_pdf_password})
参数说明数据类型
pdf_path

包含要附加的输入 PDF 文档位置和名称的字符串。

String
input_pdf_password

用于定义受保护文件的主密码的字符串。必须为主密码;用户密码将不起作用。

(默认值为 None)

String

附加受保护的 PDF 文档(即每一个文档都有不同的安全性设置)时,输出设置将取决于要附加页面的主文档。例如,如果被附加的文档未保存密码信息,但是附加的页面保存了密码信息,则生成的文档没有保存的密码信息。

要将页面添加到当前 PDF 的开头处,请使用 insertPages。

deletePages (page_range)
参数说明数据类型
page_range

用于定义要删除的一个或多个页面的字符串。通过将单个值以字符串的形式进行传递,删除单个页面(例如,"3")。可在每个值之间使用逗号来删除多个页面(例如,"3, 5, 7")。还可以应用范围(例如,"1, 3, 5-12")。

String

因为每次删除页面后,内置的 PDF 页码会自动调整,所以跟踪删除的页面十分重要。例如,把第 1 页或第 2 页删除后,第 3 页将立即成为第 2 页。如果删除第 1 页和第 2 页,则第 3 页将成为第 1 页。在使用 deletePages 后立即使用 insertPages 和一个 before_page_number 值时,您需要考虑这个问题。

insertPages (pdf_path, {before_page_number}, {input_pdf_password})
参数说明数据类型
pdf_path

包含要插入的输入 PDF 文档位置和名称的字符串。

String
before_page_number

用于在当前引用的 PDFDocument 中定义将在其前面插入新页面的页码的整数。例如,如果 before_page_value 为 1,则插入的页面将在所有页面的前面插入。

(默认值为 1)

Integer
input_pdf_password

用于定义受保护文件的主密码的字符串。必须为主密码;用户密码将不起作用。

(默认值为 None)

String

插入具有不同安全性设置的受保护 PDF 文档时,输出设置将取决于要插入页面的主文档。例如,如果被插入的页面未保存密码信息,但是插入的页面保存了密码信息,则生成的文档没有保存的密码信息。

要将页面添加到当前 PDF 的结尾处,请使用 appendPages。

saveAndClose ()

saveAndClose 方法必须用于要保留的更改。如果在执行 saveAndClose 前就已存在脚本,则不会保存更改。如果使用 PDFDocumentCreate 创建新文件,那么在运行 saveAndClose 之前磁盘中不会存在该文件。

updateDocProperties ({pdf_title}, {pdf_author}, {pdf_subject}, {pdf_keywords}, {pdf_open_view}, {pdf_layout})
参数说明数据类型
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.

  • VIEWER_DEFAULT—The application user preference when opening the file will be used.
  • USE_NONE—Only the document will display. No other panels will display.
  • USE_THUMBS—The document and the Pages panel will display.
  • USE_BOOKMARKS—The document and the Bookmarks panel will display.
  • FULL_SCREEN—The document will display in full-screen viewing mode.
  • LAYERS—The document and the Layers panel will display.
  • ATTACHMENT—The document and the Attachments panel will display.

(默认值为 USE_THUMBS)

String
pdf_layout

Specifies the Adobe Reader layout mode that will be used.

  • VIEWER_DEFAULT—The application user preference when opening the file will be used.
  • SINGLE_PAGE—Single-page mode will be used.
  • ONE_COLUMN—One-column continuous mode will be used.
  • TWO_COLUMN_LEFT—Two-column continuous mode with the first page on the left will be used.
  • TWO_COLUMN_RIGHT—Two-column continuous mode with the first page on the right will be used.
  • TWO_PAGE_LEFT—Two-page mode left will be used.
  • TWO_PAGE_RIGHT—Two-page mode right will be used.

(默认值为 SINGLE_PAGE)

String

FULL_SCREEN 的 pdf_open_view 设置将在打开 PDF 时提示关于全屏模式的警告。 将 pdf_open_view 设置为其他选项不会清除此设置,除非将 pdf_open_view 设置为 USE_NONE。

updateDocSecurity (new_master_password, {new_user_password}, {encryption}, {permissions})
参数说明数据类型
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-128—128-bit AES encryption (Acrobat 7.0 compatible) will be used.
  • AES-256-R5—256-bit AES encryption with R5 encoding (Acrobat 9.0 compatible) will be used.
  • AES-256—256-bit AES encryption (Acrobat X compatible) will be used.
  • RC4—128-bit RC4 encryption (Acrobat 5.0 compatible) will be used.
    旧版本:

    RC4 is only included for compatibility. AES encryption is recommended. RC4 will be deprecated in a future release.

(默认值为 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—All permissions will be granted.
  • ALL_MASTER—COPY, EDIT, EDIT_NOTES, and HIGH_PRINT permissions will be granted.
  • COPY—Permission to copy information from the document to the clipboard will be granted.
  • DOC_ASSEMBLY—Permission to perform page insert, delete, and rotate, as well as to create bookmarks and thumbnails will be granted.
  • EDIT—Permission to edit the document in ways other than adding or modifying text notes will be granted.
  • EDIT_NOTES—Permission to add, modify, and delete text notes, as well as fill in or sign existing form or signature fields will be granted.
  • FILL_AND_SIGN—Permission to fill in or sign existing form or signature fields will be granted.
  • HIGH_PRINT—Permission for high-quality printing will be granted.
  • PRINT—Permission to print the document will be granted.

(默认值为 ALL)

String
提示:

通过将 new_master_password 或 new_user_password 属性设置为空字符串,可以移除受保护 PDF 的密码。

代码示例

PDFDocument 示例 1

此脚本将创建新 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
PDFDocument 示例 2

以下脚本修改 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
PDFDocument 示例 3

以下脚本将设置 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
PDFDocument 示例 4

以下脚本将使用 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