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

2 comments:

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

  2. Nice Interesting blog. You are posts are just amazing and very creative. Keep on share it.

    Hotschedules login
    Firstrowsports
    Content com android browser home

    ReplyDelete