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#.

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


Step 1: Google

Google "ExportActiveView C#.net" and go to the following URL:
http://resources.esri.com/help/9.3/arcgisengine/dotnet/ae109446-1798-41aa-b878-947425b360c9.htm

Step 2: Download

Click on  "Download the C# files " link
(http://resources.esri.com/help/9.3/SampleZipsNetDtEng/ae109446-1798-41aa-b878-947425b360c9CSharp.zip)
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()
        {
            InitializeComponent();
        }

        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;
            m_ActiveView.Activate(GetDesktopWindow().ToInt32());

            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
            mapDoc.Close();
        }
    }
}

///////////////////////////////////////////////////////////////////

Step 8: ExportActiveViewCS_Net.cs

Go to ExportActiveViewCS_Net.cs line 178



Replace

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

With

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

4 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  2. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. speaker madrelingua

    ReplyDelete
  3. Interested in details on online casinos? Come to us, here is all in full details. Top online casinos in the world Best casino only at BGAOC.

    ReplyDelete