Friday, August 7, 2015

How to Export ESRI Map Document MXD File with C#

There are several ways to export MXD map document into PDF document for reporting purpose. We can use Python scripts or ArcObjects. This post will focus on how to programmatically export MXD map document into PDF using ArcObject/C#.

- ArcMap with ArcObject installed
- Visual Studio (This example use Visual Studio Express 2015)

Step 1: Google

Google "ExportActiveView" and go to the following URL:

Step 2: Download

Click on  "Download the C# files " link
Extract the zip file.

Step 3: Add a new Visual Studio Project

Run Visual Studio as administrator and open ExportActiveViewCS_Net2008.sln file

Click OK

Click OK

Step 4: On the Solutions Explorer, right click on Solution > Add > New Project

Select "Windows Forms Application" and click OK

On Solutions Explorer, right click the Windows Forms Application that we have just created and select "Set as StartUp Project"

Drag a button from the Toolbox into your form

Double click on the button

Step 5: Add Reference

On the Solution Explorer, find "References" under the Project that we have created

Right click on the References > Add Reference

Make sure that ESRI.ArcGIS. extensions are available under Assemblies > Extension

If ESRI.ArcGIS extensions are not available, check whether ArcObject for C#.Net is installed.

 Select the following Extensions and click OK

Right click on the References > Add Reference

Go to Projects and select ExportActiveViewCS_Net2008 and click OK

Step 6: Set Embed Interop Type to False

Select all ESRI.ArcGIS. references one by one and set the Embed Interop Types to False 

Step 7: The Codes

Double click on the button that we have added earlier

The codes window will appear

Add in the following codes:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        [DllImport("user32.dll", SetLastError = false)]
        static extern IntPtr GetDesktopWindow();

        public Form1()

        private void button1_Click(object sender, EventArgs e)
            ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.Desktop); //IMPORTANT bind the license

            IMapDocument mapDoc = new MapDocumentClass();
            mapDoc.Open(@"C:\sample.mxd"); //Location of the mxd file

            IActiveView m_ActiveView = mapDoc.ActiveView;

            ExportActiveViewCS_Net.ExportActiveViewCS_Net exportView = new ExportActiveViewCS_Net.ExportActiveViewCS_Net();
            exportView.ExportActiveViewParameterized(m_ActiveView, 300, 1, "PDF", @"C:\", true); //Location of the output pdf file


Step 8: ExportActiveViewCS_Net.cs

Go to ExportActiveViewCS_Net.cs line 178


public void ExportActiveViewParameterized(long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir, Boolean bClipToGraphicsExtent)


public void ExportActiveViewParameterized(IActiveView docActiveView, long iOutputResolution, long lResampleRatio, string ExportType, string sOutputDir, Boolean bClipToGraphicsExtent)

Step 9: Comment out some codes

If we hit compile and run now, we will have several errors.

To fix this, comment out some codes that we don't use:

ExportActiveViewCS_Net.cs Line 173
//ExportActiveViewParameterized (300, 1, "JPEG", exportFolder + "\\", false);

ExportActiveViewCS_Net.cs Line 193
//IActiveView docActiveView = m_hookHelper.ActiveView;

The code can be downloaded from the link below:
Source Code