We put the location of the MXD at the bottom of every map so people can find it when looking at the final exported map (PDF). Is there any way to make this a hyperlink so people can click on the link in the PDF and it will just open the MXD?
Unfortunately, no out-of-the-box solution with arcpy
and Python.
This can be done using external Python packages, I am using reportlab
and pyPDF2
. You can approach this problem in two ways:
Create a pdf file from mxd in ArcMap manually using dynamic text. You will get the path to map document in the output pdf file. Now you have to use pdf reading functions to read the text from there.
Use
arcpy
to export mxd file to pdf with no dynamic texts there at all. I would stick to this one.
The reason that you have to read the pdf file is because you cannot access the contents of a dynamic text in arcpy
:
#----------------------------------------------------------------------
def get_mxd_path_from_layout(mxd_path):
"""return text used for dynamic text `Document Path`"""
mxd = mp.MapDocument(mxd_path)
mxd_path_elem = [elem for elem in
mp.ListLayoutElements(mxd, element_type='TEXT_ELEMENT')
if elem.name == 'MapDocumentPathItem'][0]
return mxd_path_elem.text
result = get_mxd_path_from_layout(r'C:\GIS\Temp\pdfs\MyMap.mxd')
print result
>>> u'Document Path: <dyn type="document" property="path"/>'
The workflow is:
- Export map document to a pdf file.
- Create a single page pdf with the url object.
- Overlay the pdf with url object with the map pdf page.
This is essentially the same workflow as applying a watermark over a pdf page. Here are steps 1
and 2
.
import arcpy.mapping as mp
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
#----------------------------------------------------------------------
def mxd_to_pdf(mxd_path, out_pdf):
"""export mxd file to pdf"""
mxd = mp.MapDocument(mxd_path)
mp.ExportToPDF(mxd, out_pdf, data_frame='PAGE_LAYOUT')
#----------------------------------------------------------------------
def create_watermark_pdf(out_pdf, watermark_text):
"""create a single page watermark pdf with the text in the lower left corner"""
c = canvas.Canvas(out_pdf)
c.setFont("Helvetica", 6)
c.setFillColorRGB(0,0,255)
r1 = (inch/15, inch/15, 5*inch, inch/15 + 20) # this is x1,y1,x2,y2
c.linkURL(r"file://{}".format(watermark_text.replace('\\','/')), r1, thickness=0)
c.drawString(inch/15+3, inch/15 + 3, 'Disk path to mxd | {}'.format(watermark_text))
c.showPage()
c.save()
#----------------------------------------------------------------------
def main():
"""export a map document file into a single page pdf file
with an mxd path url added in the lower left corner of the pdf"""
mxd_path = r'C:\GIS\Temp\pdfs\MyMap.mxd'
mxd_to_pdf(mxd_path=mxd_path, out_pdf=r'C:\GIS\Temp\pdfs\MapImage.pdf')
create_watermark_pdf(out_pdf=r'C:\GIS\Temp\pdfs\MapWatermark.pdf',
watermark_text=mxd_path)
if __name__ == '__main__':
main()
After this, you have two pdf files, one with the map exported to pdf, and another one with the url object (already working url - clicking on this url will trigger opening ArcMap with the mxd file).
Lower left part of the pdf page with map
Lower left part of the pdf page with url
Here is the step 3
:
import PyPDF2
watermark_pdf_path = r"C:\GIS\Temp\pdfs\MapWatermark.pdf"
pdf_path = r"C:\GIS\Temp\pdfs\MapImage.pdf"
map_pdf = open(pdf_path, 'rb')
watermark_pdf = open(watermark_pdf_path, 'rb')
pdfReader = PyPDF2.PdfFileReader(map_pdf)
pdfWatermarkReader = PyPDF2.PdfFileReader(watermark_pdf)
pdfWriter = PyPDF2.PdfFileWriter()
map_pdf_first_page = pdfReader.getPage(0)
map_pdf_first_page.mergePage(pdfWatermarkReader.getPage(0))
pdfWriter.addPage(map_pdf_first_page)
resultPdfFile = open(r"C:\GIS\Temp\pdfs\Complete.pdf", 'wb')
pdfWriter.write(resultPdfFile)
map_pdf.close()
watermark_pdf.close()
resultPdfFile.close()
Now you can click the link and you will be prompted to allow to open ArcMap.
-
Thanks for the detailed instructions. I'll go ahead and give this a try. – kbrenn89 Mar 31 '17 at 16:01
In PDF Tools there is an option to create a link in a PDF to open another document. You would have to create this link manually or sort it out with something like Python. https://stackoverflow.com/questions/28796832/add-links-in-pdf
-
1
-
The link you posted to SO doesn't contain any helpful information on how to create a new link with URL in a body of pdf. – Alex Tereshenkov Mar 31 '17 at 13:23
<a href='<dyn type="document" property="path"/>'>click me</a>
– KHibma Mar 30 '17 at 15:34