About the Publish a map Sample
[C#]
PublishMap.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Publisher;
using ESRI.ArcGIS.PublisherUI;
using ESRI.ArcGIS.Carto;
namespace PublishMap2008CSharp
{
public class PublishMap : ESRI.ArcGIS.Desktop.AddIns.Button
{
public PublishMap()
{
}
protected override void OnClick()
{
//Enable publisher extension
if (EnablePublisherExtension())
{
System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
IMxDocument mapDoc = ArcMap.Document;
IPMFPublish3 publishMaps = new PublisherEngineClass();
//Accept the default settings when the map is published.
IPropertySet settings = publishMaps.GetDefaultPublisherSettings();
IArray orderedMaps = new ArrayClass();
//Loop over all the maps in the map document and each one to an Array object
for (int i = 0; i <= mapDoc.Maps.Count - 1; i++)
{
orderedMaps.Add(mapDoc.Maps.get_Item(i));
}
try
{
//Create the PMF file using the current settings and the map order specified in the IArray parameter
//It will be written to C:\\PublishedMap.pmf and the data will be referenced using relative paths.
publishMaps.PublishWithOrder(orderedMaps, mapDoc.PageLayout, mapDoc.ActiveView, settings,
true, "C:\\PublishedMap.pmf");
//Report outcome to the user
string mapdocTitle = ((IDocument)ArcMap.Document).Title;
string msg = string.Empty;
if (orderedMaps.Count == 1)
{
msg = "The map in " + mapdocTitle + " has been published successfully";
}
else
{
msg = "The maps in " + mapdocTitle + " have been published successfully";
}
MessageBox.Show(msg);
}
catch (Exception ex)
{
MessageBox.Show("Error Publishing Map: " + ex.Message, "Error");
}
System.Windows.Forms.Cursor.Current = Cursors.Default;
}
ArcMap.Application.CurrentTool = null;
}
protected override void OnUpdate()
{
Enabled = ArcMap.Application != null;
}
private bool EnablePublisherExtension()
{
bool checkedOutOK = false;
try
{
IExtensionManager extMgr = new ExtensionManagerClass();
IExtensionManagerAdmin extAdmin = (IExtensionManagerAdmin)extMgr;
UID uid = new UID();
uid.Value = "esriPublisherUI.Publisher";
object obj = 0;
extAdmin.AddExtension(uid, ref obj);
IExtensionConfig extConfig = (IExtensionConfig)extMgr.FindExtension(uid);
if ((!(extConfig.State == esriExtensionState.esriESUnavailable)))
{
//This checks on the extension
extConfig.State = esriExtensionState.esriESEnabled;
checkedOutOK = true;
}
}
catch (Exception)
{
MessageBox.Show("Publisher extension has failed to check out.", "Error");
}
return checkedOutOK;
}
}
}
[Visual Basic .NET]
PublishMap.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Windows.Forms
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.Publisher
Imports ESRI.ArcGIS.PublisherUI
Imports ESRI.ArcGIS.Carto
Public Class PublishMap
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Public Sub New()
End Sub
Protected Overrides Sub OnClick()
'Enable publisher extension
If (EnablePublisherExtension()) Then
System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
Dim mapDoc As IMxDocument = My.ArcMap.Document
Dim publishMaps As IPMFPublish3 = New PublisherEngineClass()
'Accept the default settings when the map is published
Dim settings As IPropertySet = publishMaps.GetDefaultPublisherSettings()
Dim orderedMaps As IArray = New ArrayClass()
'Loop over all the maps in the map document and each one to an Array object
For i As Integer = 0 To mapDoc.Maps.Count - 1
orderedMaps.Add(mapDoc.Maps.Item(i))
Next
Try
'Create the PMF file using the current settings and the map order specified in the IArray parameter
'It will be written to C:\\PublishedMap.pmf and the data will be referenced using relative paths.
publishMaps.PublishWithOrder(orderedMaps, mapDoc.PageLayout, mapDoc.ActiveView, settings, _
True, "C:\\PublishedMap.pmf")
'Report outcome to the user
Dim mapdocTitle As String = DirectCast(My.ArcMap.Document, ESRI.ArcGIS.Framework.IDocument).Title
Dim msg As String = String.Empty
If (orderedMaps.Count = 1) Then
msg = "The map in " + mapdocTitle + " has been published successfully"
Else
msg = "The maps in " + mapdocTitle + " have been published successfully"
End If
MessageBox.Show(msg)
Catch ex As Exception
MessageBox.Show("Error Publishing Map: " + ex.Message, "Error")
End Try
System.Windows.Forms.Cursor.Current = Cursors.Default
End If
My.ArcMap.Application.CurrentTool = Nothing
End Sub
Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub
Private Function EnablePublisherExtension() As Boolean
Dim checkedOutOK As Boolean = False
Try
Dim extMgr As IExtensionManager = New ExtensionManagerClass()
Dim extAdmin As IExtensionManagerAdmin = DirectCast(extMgr, IExtensionManagerAdmin)
Dim ud As UID = New UID()
ud.Value = "esriPublisherUI.Publisher"
Dim obj As Object = 0
extAdmin.AddExtension(ud, obj)
Dim extConfig As ESRI.ArcGIS.esriSystem.IExtensionConfig = DirectCast(extMgr.FindExtension(ud), ESRI.ArcGIS.esriSystem.IExtensionConfig)
If Not (extConfig.State = ESRI.ArcGIS.esriSystem.esriExtensionState.esriESUnavailable) Then
'This checks on the extension
extConfig.State = ESRI.ArcGIS.esriSystem.esriExtensionState.esriESEnabled
checkedOutOK = True
End If
Catch ex As Exception
MessageBox.Show("Publisher extension has failed to check out.", "Error")
End Try
Return checkedOutOK
End Function
End Class