Tuesday, 28 November 2017

arcgis desktop - Writing hidden information to PDF file from ArcMap?


I'm usually creating maps for (different aspects of) reports of large infrastructure projects. Layers and attributes tend to change back and forth as time goes by, and I'm forced to create new versions of these maps regularly.



The problem is that in each project I have some 50-60 mxd files, some with very small changes. What makes it even more complicated, all of the produced maps should have the same date (and an overall very strict layout) according to that specific project's end date. Even with a very thought-through naming convention, it can sometimes be a real pain to find out which mxd belongs to a certain map that I am tasked to update.


Is there a way to automatically write the path of the mxd (and preferably the date of export) to a pdf and/or a png file somewhere in the metadata when exporting from ArcMap?


The information should not be visible when printed, nor could the file name be used for this (since it will change multiple times), but should also not be too complicated to find when looking for it. The process of exporting a pdf/png in this way should be reasonably fast, since I'll be doing it several times a day.


Does such metadata exist within pdf files and/or png files, and how can I access it?



Answer



I had a similar problem with this question: Creating bookmarked PDF from export script


The following script will export an MXD to PDF, and modify the PDFs properties (via output.addMetadata) using the PyPDF2 module.


import sys
sys.path.insert(0, r"H:\NetworkShared_PythonModules")
import PyPDF2, os, arcpy


mxdPath = r"C:\temp\MyMap.mxd"
mxd = arcpy.mapping.MapDocument(mxdPath)
PDFPath = r"C:\temp\MyPDF.pdf"
arcpy.mapping.ExportToPDF(mxd, PDFPath)

output = PyPDF2.PdfFileMerger()

bookMarkText = "Insert BookMark Text Here"
inputPage = PyPDF2.PdfFileReader(open(PDFPath, 'rb'))

output.addMetadata({u'/Keywords': u'Path to MXD' + mxdPath, u'/Author': u'UserName'})
output.append(inputPage, bookMarkText)

outputStream = file(PDFPath, "wb")
output.write(outputStream)
outputStream.close()
del outputStream, output, mxd, inputPage

I haven't tried, but I'm sure this could be done in a loop with several MXDs.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...