Print and Export Maps from ArcMap with ArcPy Mapping
Printing and exporting maps is obviously one of the most commonly performed ArcGIS tasks. The new Arcpy.mapping module provides several functions that you can use to automate your routine print and export tasks. In this post we’ll examine Arcpy.mapping functions you can use for printing and exporting either maps from either the data or layout view in ArcMap.
Arcpy.mapping provides a PrintMap() function for printing page layouts or data frames within ArcMap and a host of Export functions that export the page layout or a data frame to various image formats and PDF files. Before calling PrintMap() it is common practice to call the ListPrinterNames() function which returns a list of the available printers for the local computer. A particular printer can then be found by iterating the list of printers and used as input to the PrintMap() function.
PrintMap() can print either a specific data frame or the page layout of a map document. By default this function will use the printer saved with the map document or, if not present in the map document, the default system printer. As I mentioned you can also use ListPrinterNames() to get a list of the available printers and select one of these printers as input to PrintMap(). In the code example on below PrintMap() uses the default printer as specified through the use of double quotes as the second parameter to PrintMap().
You can use one of the many Export functions provided by Arcpy.mapping to export a data frame or page layout to an image file. Available formats include AI, BMP, EMF, EPS, GIF, JPEG, SVG, and TIFF. The parameters provided for each function will vary depending upon the type of image. Examples of these function names include ExportToJPEG, ExportToGIF, ExportToBMP, and all the others you see listed here.
You can also export your data frames or page layout to PDF format with ExportToPDF(). PDF is obviously a very popular interchange format so you will likely need to become familiar with this function. Calling ExportToPDF() is much the same as calling any of the image export functions since it takes an mxd and path to output the file along with optional properties for the PDF.
In addition to exporting your maps to PDF you can also manipulate existing PDF documents or create new PDF documents. You can merge pages, set document open behavior, add file attachments, and create or change document security settings. The PDFDocumentOpen() function is used to open an existing PDF file for manipulation. PDFDocumentCreate() creates a new PDF document. These functions are often used in the creation of map books.
You’ll need to use PDFDocumentCreate to create a new PDF document by providing a path and filename for the document. The PDF is not actually created on disk until you insert or append pages and then call PDFDocument.saveAndClose(). The appendPages() and insertPages() functions are used to insert and append pages.
PDFDocumentOpen accepts a parameter that specifies the path to a PDF file and returns an instance of PDFDocument. Once open you can make modifications to PDF file properties, add or insert files, and attach documents. Make sure you call PDFDocument.saveAndClose() after all operations to save the changes to disk.
A number of properties can be set on a PDF Document through the PDFDocument object including getting a page count, attaching files, updating title, author, subject, keywords, open behavior, and the layout. You can also update the document security by calling PDFDocument.updateDocSecurity() to set a password, encryption, and security restrictions.
Other ArcPy Mapping articles.
- Using ArcPy Mapping to Add and Remove Layers in a Map Document
- Working with Layout Elements in the ArcPy Mapping Module
- Introducing the ArcPy Mapping Module in ArcGIS 10
- Free E-Learning Module from the ArcGIS 10 Version of our GIS Programming 101 Course
Want to learn more about the ArcPy Mapping module and other programming ArcGIS Desktop with Python topics? The next session of our Internet based, instructor led course “GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS” begins October 25th, 2010. You can still register even though class has already begun.