PDFDocument

Summary

Allows manipulation of PDF documents, including facilities for merging pages, setting document open behavior, adding file attachments, and creating or changing document security settings

Discussion

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.

Syntax

PDFDocument (pdf_path)
ParameterExplanationData Type
pdf_path

A string that specifies the path and file name for the resulting PDF file when the saveAndClose method is called.

String

The PDFDocumentCreate function receives a path to determine the save location and file name where a new PDF file will be created. However, no PDF file will be created until subsequent steps are performed to insert or append pages and save the PDF file. PDFDocumentCreate will return a PDFDocument object that your script should then manipulate and save. A common scenario for using this function is the creation of a PDF map book. The steps typically involve exporting a number of separate PDF files from map documents, creating a new PDFDocument object, appending content from the exported PDF files and other documents, and saving the final PDF map book.

Please note that it is not possible to create blank PDF files, nor does the PDFDocumentCreate function add any blank pages to the document contents. For the saveAndClose method to successfully create a file, content must be added to the PDFDocument object using the appendPages or insertPages methods.

For more discussion on how to create map books, see the Building Map Books with ArcGIS help topic.

PDFDocumentCreate example

This script will create a new PDF document, append the contents of three 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")
pdfDoc.appendPages(r"C:\Project\ContactInfo.pdf")

#Commit changes and delete variable reference
pdfDoc.saveAndClose()
del pdfDoc

Properties

PropertyExplanationData Type
pageCount
(Read Only)

Returns an integer that represents the total number of pages in the PDF document

Long

Method Overview

MethodExplanation
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.

Methods

appendPages (pdf_path, {input_pdf_password})
ParameterExplanationData Type
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

(The default value is 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.

attachFile (file_path, {description})
ParameterExplanationData Type
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.

insertPages (pdf_path, {before_page_number}, {input_pdf_password})
ParameterExplanationData Type
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.

(The default value is 1)

String
input_pdf_password

A string that defines the master password to a protected file.

(The default value is 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.

saveAndClose ()

The saveAndClose method must be used for changes to be maintained. If a script exits before saveAndClose is executed, changes will not be saved.

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

A string defining the document title, a PDF metadata property.

(The default value is None)

String
pdf_author

A string defining the document author, a PDF metadata property.

(The default value is None)

String
pdf_subject

A string defining the document subject, a PDF metadata property.

(The default value is None)

String
pdf_keywords

A string defining the document keywords, a PDF metadata property.

(The default value is 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.

  • VIEWER_DEFAULTUses the application user preference when opening the file
  • USE_NONEDisplays the document only; does not show other panels
  • USE_THUMBSDisplays the document plus the Pages panel
  • USE_BOOKMARKSDisplays the document plus the Bookmarks panel
  • FULL_SCREENDisplays the document in full-screen viewing mode
  • LAYERSDisplays the document plus the layers panel
  • ATTACHMENTDisplays the document plus the attachment panel

(The default value is USE_THUMBS)

String
pdf_layout

A string or number that will define the initial view mode to trigger when the PDF file is viewed.

  • DONT_CAREUses the application user preference when opening the file
  • SINGLE_PAGEUses single-page mode
  • ONE_COLUMNUses one-column continuous mode
  • TWO_COLUMN_LEFTUses two-column continuous mode with first page on left
  • TWO_COLUMN_RIGHTUses two-column continuous mode with first page on right
  • TWO_PAGE_LEFTUses two-page mode left
  • TWO_PAGE_RIGHTUses two-page mode right

(The default value is 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.

updateDocSecurity (new_master_password, {new_user_password}, {encryption}, {permissions})
ParameterExplanationData Type
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.

(The default value is None)

String
encryption

A string that defines the encryption technique used on the PDF.

  • "AES_V1"Uses 128-bit AES encryption (Acrobat 7.0 compatible)
  • "AES_V2"Uses 256-bit AES encryption (Acrobat 9.0 compatible)
  • "RC4"Uses 128-bit RC4 encryption (Acrobat 5.0 compatible)

(The default value is 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"Grants all permissions
  • "ALL_MASTER"Grants permissions for COPY, EDIT, EDIT_NOTES, and HIGH_PRINT
  • "COPY"Grants permission to copy information from the document to the clipboard
  • "DOC_ASSEMBLY"Grants permission to perform page insert, delete, and rotate, and allows creation of bookmarks and thumbnails
  • "EDIT"Grants permission to edit the document in ways other than adding or modifying text notes
  • "EDIT_NOTES"Grants permission to add, modify, and delete text notes
  • "FILL_AND_SIGN"Grants permission to fill in or sign existing form or signature fields
  • "HIGH_PRINT"Grants permission for high-quality printing
  • "OPEN"Grants permission to open or decrypt the document
  • "PRINT"Grants permission to print the document
  • "SECURE"Grants permission to change the document's security settings

(The default value is 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.

Code Sample

PDFDocument example 1

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
PDFDocument example 2

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
PDFDocument example 3

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
PDFDocument example 4

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

11/21/2011