This sample demonstrates printing a map using a PrintTask. To use the sample, specify the print settings (Layout template and Format) using the dropdowns and click the Export Map button to generate a printout of the map. The printout will be displayed as appropriate for the format chosen. In the code-behind, a PrintTask is used to generate a printable file from the map. The layout and format will match those specified in the sample, and a URL to the printable file is returned. The sample then navigates to that URL to show the printable map.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.ExportWebMap "
xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:esri = " http://schemas.esri.com/arcgis/client/2009 " >
< Grid x:Name = " LayoutRoot " Background = " White " >
< esri : Map Name = " MyMap " UseAcceleratedDisplay = " True " Extent = " -10929488.234,4525208.388,-10906776.553,4535252.104 " >
< esri : ArcGISTiledMapServiceLayer Url = " http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer " />
< esri : ArcGISDynamicMapServiceLayer Url = " http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSFields/MapServer " />
< esri : FeatureLayer Mode = " OnDemand " Url = " http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSWells/MapServer/0 "
DisableClientCaching = " True " />
</ esri : Map >
< Border BorderBrush = " Gray " BorderThickness = " 1 " Background = " White " HorizontalAlignment = " Left "
VerticalAlignment = " Top " Margin = " 15 " Padding = " 10 " >
< Border.Effect >
< DropShadowEffect />
</ Border.Effect >
< Grid >
< Grid.ColumnDefinitions >
< ColumnDefinition />
< ColumnDefinition />
</ Grid.ColumnDefinitions >
< Grid.RowDefinitions >
< RowDefinition />
< RowDefinition />
< RowDefinition />
</ Grid.RowDefinitions >
< TextBlock Text = " LayoutTemplates " Grid.Row = " 0 " Grid.Column = " 0 " Margin = " 2 " />
< ComboBox x:Name = " LayoutTemplates " Grid.Row = " 0 " Grid.Column = " 1 " Width = " 100 " Margin = " 2 " >
< ComboBox.ItemTemplate >
< DataTemplate >
< TextBlock Text = " {Binding} " />
</ DataTemplate >
</ ComboBox.ItemTemplate >
</ ComboBox >
< TextBlock Text = " Formats " Grid.Row = " 1 " Grid.Column = " 0 " Margin = " 2 " />
< ComboBox x:Name = " Formats " Grid.Row = " 1 " Grid.Column = " 1 " Width = " 100 " Margin = " 2 " >
< ComboBox.ItemTemplate >
< DataTemplate >
< TextBlock Text = " {Binding} " />
</ DataTemplate >
</ ComboBox.ItemTemplate >
</ ComboBox >
< Button x:Name = " Print " Content = " Export Map " Click = " ExportMap_Click "
Grid.Row = " 2 " Grid.Column = " 0 " Margin = " 2 " />
</ Grid >
</ Border >
</ Grid >
</ UserControl >
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Printing;
using System;
using System.Diagnostics;
namespace ArcGISWPFSDK
{
public partial class ExportWebMap : UserControl
{
PrintTask printTask;
public ExportWebMap()
{
InitializeComponent();
printTask = new PrintTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task" );
printTask.DisableClientCaching = true ;
printTask.ExecuteCompleted += printTask_PrintCompleted;
printTask.GetServiceInfoCompleted += printTask_GetServiceInfoCompleted;
printTask.GetServiceInfoAsync();
}
private void printTask_GetServiceInfoCompleted(object sender, ServiceInfoEventArgs e)
{
LayoutTemplates.ItemsSource = e.ServiceInfo.LayoutTemplates;
Formats.ItemsSource = e.ServiceInfo.Formats;
}
private void printTask_PrintCompleted(object sender, PrintEventArgs e)
{
System.Uri uri;
uri = e.PrintResult.Url;
try
{
Process.Start(uri.ToString());
}
catch (Exception ex)
{
string error = ex.Message;
MessageBox.Show(error);
throw ;
}
}
private void ExportMap_Click(object sender, RoutedEventArgs e)
{
if (printTask == null || printTask.IsBusy) return ;
PrintParameters printParameters = new PrintParameters(MyMap)
{
ExportOptions = new ExportOptions() { Dpi = 96, OutputSize = new Size(MyMap.ActualWidth, MyMap.ActualHeight) },
LayoutTemplate = (string )LayoutTemplates.SelectedItem ?? string .Empty,
Format = (string )Formats.SelectedItem,
};
printTask.ExecuteAsync(printParameters);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.Printing
Imports System.Diagnostics
Namespace ArcGISWPFSDK
Partial Public Class ExportWebMap
Inherits UserControl
Private printTask As PrintTask
Public Sub New ()
InitializeComponent()
printTask = New PrintTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task" )
printTask.DisableClientCaching = True
AddHandler printTask.ExecuteCompleted, AddressOf printTask_PrintCompleted
AddHandler printTask.GetServiceInfoCompleted, AddressOf printTask_GetServiceInfoCompleted
printTask.GetServiceInfoAsync()
End Sub
Private Sub printTask_GetServiceInfoCompleted(sender As Object , e As ServiceInfoEventArgs)
LayoutTemplates.ItemsSource = e.ServiceInfo.LayoutTemplates
Formats.ItemsSource = e.ServiceInfo.Formats
End Sub
Private Sub printTask_PrintCompleted(sender As Object , e As PrintEventArgs)
Dim uri As System.Uri
uri = e.PrintResult.Url
Try
Process.Start(uri.ToString())
Catch ex As Exception
Dim [error ] As String = ex.Message
MessageBox.Show([error ])
Throw
End Try
End Sub
Private Sub ExportMap_Click(sender As Object , e As RoutedEventArgs)
If printTask Is Nothing OrElse printTask.IsBusy Then
Return
End If
Dim printParameters As New PrintParameters(MyMap) With { _
.ExportOptions = New ExportOptions() With { _
.Dpi = 96, _
.OutputSize = New Size(MyMap.ActualWidth, MyMap.ActualHeight) _
}, _
.LayoutTemplate = If (DirectCast (LayoutTemplates.SelectedItem, String ), String .Empty), _
.Format = DirectCast (Formats.SelectedItem, String ) _
}
printTask.ExecuteAsync(printParameters)
End Sub
End Class
End Namespace
5/16/2014