1

I regularly use an export script to create multi-page PDFs from Data-Driven-Pages based Map-books. I am able to export and append each individual page to the PDF, and save the PDF in the single-page layout with the "Page Thumbnails" pane opened. I need to be able to (Pythonically) set the Bookmark text for each page within the PDF. I can do this manually from Adobe, but it would save a lot of time and headache if it could be automated.

Here is the script I use, at its most basic level without any bells or whistles. At the bottom of the script is where I set the page-layout (pdf_open_view="USE_THUMBS"). I can use "USE_BOOKMARKS" but this doesn't actually apply any bookmark text to the individual pages.

import arcpy, os
mxdPath = r"C:\temp\MyMapBook.mxd" 
tempMap = arcpy.mapping.MapDocument(mxdPath)
tempDDP = tempMap.dataDrivenPages

outDir      = r"C:\temp"   #PDF Folder where the individual pages and Combo are created
comboPDF    = r"\MultiPage_PDF.pdf"        #Name of Combo PDF
finalpdf_filename = os.path.join(outDir + comboPDF)
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)

for pgIndex in range(1, tempDDP.pageCount + 1):
    tempDDP.currentPageID = pgIndex
    figName = str(tempDDP.pageRow.sdsFeatureName)
    figNumber = str(tempDDP.pageRow.Fig_Num_2015FSP)
    pageName = "Page-" + figNumber + "_" + figName + ".pdf"
    individualPagePDF = os.path.join(outDir + "\\" + pageName)

    #do something to the page before exporting. e.g. add inset map, add table etc.

    ###################################################################
    tempDDP.exportToPDF(individualPagePDF, "CURRENT") # export to signle page PDF
    finalPdf.appendPages(individualPagePDF) # append the page to the "combo" PDF
    print pageName + " added to final mapbook pdf"                    
    ###################################################################

    #undo whatever page modifications you just did before the export

##### Delete Objects
del tempMap, tempDDP

finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS",pdf_layout="SINGLE_PAGE")
finalPdf.saveAndClose()
  • I have a similar workflow where I create a multipage PDF map atlas from an MXD with the Data Driven Pages module. I then add PDF metadata, bookmarks and control the page display with a pdfmarks postscript file that is combined with my map atlas using Ghostscript (ghostscript.com). Check out the "Embedding PDFmarks" section of this page (milan.kupcevic.net/ghostscript-ps-pdf) for a quick primer. All of this can be scripted fairly easily. – Brent Edwards Mar 7 '17 at 15:43
2

The PyPDF2 module works as needed in this situation. The script no longer uses arcpy.mapping.PDFDocumentCreate. The combo-PDF is now created using the PyPDF2 module. In the below script, it begins with the output = PyPDF2.PdfFileMerger().

Because multiple users need to access the module across the office network, installing the module on my machine would not have worked. Instead, the PyPDF2 module was placed in a network folder, and sys.path.insert was added to the script with the location to the network path.

Here is the working script that bookmarks each page within the combo-PDF.

import sys
sys.path.insert(0, r"H:\NetworkShared_PythonModules") # insert a path to the folder containing the PyPDF2 module
import PyPDF2, os, arcpy

mxdPath = r"C:\temp\MyMapBook.mxd" 
tempMap = arcpy.mapping.MapDocument(mxdPath)
tempDDP = tempMap.dataDrivenPages


outDir      = r"C:\temp"   #PDF Folder where the individual pages and Combo will be created
comboPDF    = r"MultiPage_PDF.pdf"        #Name of Combo PDF
finalpdf_filename = os.path.join(outDir, comboPDF)
output = PyPDF2.PdfFileMerger()

for pgIndex in range(1, tempDDP.pageCount + 1):
      tempDDP.currentPageID = pgIndex
      figName = str(tempDDP.pageRow.sdsFeatureName)
      figNumber = str(tempDDP.pageRow.Fig_Num_2015FSP)
      pageName = "Appendix Page-" + figNumber + "_" + figName + ".pdf"
      individualPagePDF = os.path.join(outDir, pageName)
      bookMarkText = "Figure A-" + figNumber + " --- " + figName

      #do something to the page before exporting.  e.g. add inset map, add table etc.

      ###################################################################
      tempDDP.exportToPDF(individualPagePDF, "CURRENT") # export to signle page PDF
      IndividualPageInput = PyPDF2.PdfFileReader(open(individualPagePDF, 'rb'))
      output.append(IndividualPageInput, bookMarkText)
      print pageName + " added to final mapbook pdf"                    
      ###################################################################

      #undo whatever page modifications you just did before the export

outputStream = file(finalpdf_filename, "wb")
output.setPageLayout("/SinglePage")#open the pdf to single page layout
output.setPageMode("/UseOutlines")#open the pdf with the bookMarks pane open
################################
output.write(outputStream)
outputStream.close()
del outputStream, output

##### Delete Objects
del tempMap, tempDDP
  • Did you see anything that might also help answer gis.stackexchange.com/questions/121651/… ? – PolyGeo Mar 1 '17 at 5:21
  • You can, in fact, edit a PDFs properties using the PyPDF2 module. I posted an answer on the link page. – Kegger Mar 2 '17 at 23:13
  • I've run out of votes for today so when I get my next batch in about 45 mins I'll upvote that answer. In the meantime I have tweeted it to my followers so hopefully some votes may flow from that. – PolyGeo Mar 2 '17 at 23:26
1

In the PDFDocument help it makes two mentions of bookmarks:

USE_BOOKMARKS —Displays the document plus the Bookmarks panel

and

"DOC_ASSEMBLY" —Grants permission to perform page insert, delete, and rotate, and allows creation of bookmarks and thumbnails

I suspect that the only support for bookmarks is when ArcPy's appendPages or insertPages methods are used to merge in existing PDF files which have bookmarks embedded within them.

  • 1
    If it is possible to export the individual pages with their own bookmark text, I would love to try it. I don't see any mention of this in the arcpy.mapping.ExportToPDF documentation. Currently looking into the PyPDF or PyPDF2 modules. They appear to hold some promise. Will post findings here. – Kegger Mar 1 '17 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.