Creating a map book with facing pages
ArcGIS provides all the tools you need to create map books in printed or Adobe PDF format. A map book is a collection of pages printed or exported together. Many of the pages contain maps, but other pages may be dedicated to text, tabular information, tables of contents, or title pages and other content.
Facing pages allow the map author to account for the book "gutter." The gutter is the space required to allow for binding book pages together. Often, this is a map book that contains a reference series covering a succession of map extents, just as a reference map book does. However, unlike a simple reference series, this map book utilizes the layouts of two map documents, one for the left page and one for the right. The series extents are defined using Data Driven Pages. Create the same set of Data Driven Pages in each map document. The arcpy.mapping Python script uses both map documents and assembles the left and right pages into the final PDF document in the proper order.
The example above shows a topographic map book for Arenac County, Michigan, with facing pages. Notice that the odd-numbered map pages (for example, page 3) have a layout alignment such that all page elements are shifted to the left. Even-numbered map pages (for example, page 4) are aligned to the right. This is to allow space for the book binding. Also, page numbers and the locator map have been located for each map layout so that they are on the outside of the page. Each page alignment (both left and right) is based on a separate ArcMap document. You can create this document using Data Driven Pages and an arcpy.mapping Python script.
For more information, see Creating Data Driven Pages.
This example has the following assumptions:
- You have two existing map documents with Data Driven Pages enabled where one document is set up for the left-hand pages and the other document is set up for the right-hand pages.
This code example works with the simple reference series map book example shown in Building map books with ArcGIS.You can re-create this map book by following the steps outlined in these help topics:Starting the map bookCreating grid index featuresModifying the Grid Index Features layerEnabling Data Driven PagesCreating a locator map for a map bookAdding dynamic text for a map bookExporting a map bookOnce you have created a map with Data Driven Pages, you need to create a copy of the map document. When you have two copies, you can reorient the map elements for each document such that one layout is oriented on the left and the other layout is oriented on the right. Make sure you name the map documents such that you can distinguish which is for the left side and which is for the right side of the final map book.
Since the map book displays page numbers, you should make sure that the starting page number for the Data Driven Pages accommodates all the pages of the final map book that precedes them. In this example, there are two pages that precede the map pages. Therefore, the map pages start with page 3. Make sure that 3 is the value for Starting Page Number in the Setup Data Driven Pages dialog box.
- You have an existing PDF file for the map book title page.
- You have an existing PDF file containing the overview map page.
You can create title and overview map pages using ArcMap. Just author the content for each page and export each to a separate PDF.
Once you have a map document and PDF files ready, you can run the code below to create your final map book PDF. You can run the code in the Python window or in a stand-alone Python application.
Though the specific code in this topic applies to the example map book above, you can also apply the procedures and tips presented here to your own map books.
Create a map book PDF with facing pages.
import arcpy, os # Create an output directory variable # outDir = r"C:\temp\MBExample\final_output" # Create a new, empty pdf document in the specified output directory # finalpdf_filename = outDir + r"\FinalMB.pdf" if os.path.exists(finalpdf_filename): os.remove(finalpdf_filename) finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename) # Add the title page to the pdf # finalPdf.appendPages(r"C:\temp\MBExample\ancillary_pages\TitlePage.pdf") # Add the index map to the pdf # finalPdf.appendPages(r"C:\temp\MBExample\maps\IndexMap.pdf") # Create Facing Pages for the map book # Create pages for left-hand side of the book # mxdPathLeft = r"C:\temp\MBExample\maps\Arenac County MB Left.mxd" tempMapLeft = arcpy.mapping.MapDocument(mxdPathLeft) tempDDPLeft = tempMapLeft.dataDrivenPages # Loop creates individual pdf's for odd numbered pages # for pgNumLeft in range(1, tempDDPLeft.pageCount + 1, 2): temp_filename = r"C:\temp\MBExample\temp_pdfs\MB_" + \ str(pgNumLeft) + ".pdf" if os.path.exists(temp_filename): os.remove(temp_filename) tempDDPLeft.exportToPDF(temp_filename, "RANGE", pgNumLeft) # Create pages for right-hand side of the book # mxdPathRight = r"C:\temp\MBExample\maps\Arenac County MB Right.mxd" tempMapRight = arcpy.mapping.MapDocument(mxdPathRight) tempDDPRight = tempMapRight.dataDrivenPages # Loop creates individual pdf's for even numbered pages # for pgNumRight in range(2, tempDDPRight.pageCount + 1, 2): temp_filename = r"C:\temp\MBExample\temp_pdfs\MB_" + \ str(pgNumRight) + ".pdf" if os.path.exists(temp_filename): os.remove(temp_filename) tempDDPRight.exportToPDF(temp_filename, "RANGE", pgNumRight) # Append right and left-hand pages together in proper order # for pgNum in range(1, tempDDPLeft.pageCount + 1): print "Page", pgNum, "of", tempDDPLeft.pageCount tempPDF = r"C:\temp\MBExample\temp_pdfs\MB_" + str(pgNum) + ".pdf" finalPdf.appendPages(tempPDF) # Update the properties of the final pdf # finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS", pdf_layout="SINGLE_PAGE") # Save your result # finalPdf.saveAndClose() # Delete variables # del finalPdf, mxdPathLeft, mxdPathRight, tempDDPLeft, tempDDPRight, tempMapLeft, tempMapRight, tempPDF
The first lines of code import the necessary modules; create an output location variable where the final map book PDF will be saved; and create a new, empty PDF document in the specified output location folder. This PDF is the final output for this script.
Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error. A string literal can also be used by placing the letter r before a string containing a backslash so it is interpreted correctly.
For more information, see the PDFDocument class of arcpy.mapping.
The next lines of code add the title page and the overview map page to the final PDF. This example assumes that you have existing PDF documents that can be used for title and overview map pages.
Next, the code creates Data Driven Pages for the map document with the layout aligned to the left. PDFs are created using these Data Driven Pages for each odd-numbered page. This is then repeated for the pages aligned to the right where PDFs are created for the even-numbered pages.
If you are using the Python window to run this code, you should know how to enter multiple commands for the loop.
To enter lines after the first line without executing the code block, after entering the first line, hold down the CTRL key and press ENTER. The cursor moves to a secondary prompt (...) in the Python window, and an additional line of code can be entered. When finished entering all commands in this manner, press ENTER twice to execute the entire code block.
Next, the left-hand and right-hand pages need to be appended to the final PDF in the proper order.
Finally, the code updates the properties, then saves and closes the final PDF.