Server Printing

Download Samples Repository

Description

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 using the systems default browser.

"Desktop" "Store" Available for Desktop, Store

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.ServerPrinting"
             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/runtime/2013">
    <Grid x:Name="layoutGrid">
        <esri:MapView x:Name="MyMapView">
			<esri:Map InitialViewpoint="-10929488,4525208,-10906777,4535252,3857">
				<esri:ArcGISTiledMapServiceLayer 
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
                <esri:ArcGISDynamicMapServiceLayer 
                    ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSFields/MapServer" />
                <esri:FeatureLayer>
                    <esri:ServiceFeatureTable Mode="OnDemand" 
                        ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSWells/MapServer/0" />
                </esri:FeatureLayer>
            </esri:Map>
        </esri:MapView>

        <Border Background="White" BorderBrush="Black" BorderThickness="2" Width="350"
                HorizontalAlignment="Right" VerticalAlignment="Top" Margin="30" Padding="20">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                
                <TextBlock Text="LayoutTemplate:" Grid.Row="0" Grid.Column="0" Margin="2" VerticalAlignment="Center"/>
                <ComboBox x:Name="comboLayout" Grid.Row="0" Grid.Column="1" Margin="2">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                <TextBlock Text="Format:" Grid.Row="1" Grid.Column="0" Margin="2" VerticalAlignment="Center"/>
                <ComboBox x:Name="comboFormat" Grid.Row="1" Grid.Column="1" Margin="2">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                
                <Button Content="Export Map" Click="ExportMap_Click" 
                        Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="30,12,30,0"/>
                
                <ProgressBar x:Name="progress" Grid.Row="3" Grid.ColumnSpan="2" 
                             IsIndeterminate="True" Margin="12,12,12,0" Visibility="Collapsed" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Tasks.Printing;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// 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 using the systems default browser.
    /// </summary>
    /// <title>Server Printing</title>
    /// <category>Printing</category>
    public partial class ServerPrinting : UserControl
    {
        private PrintTask _printTask;

        /// <summary>Construct Server Printing sample control</summary>
        public ServerPrinting()
        {
            InitializeComponent();

            _printTask = new PrintTask(
                new Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"));

            MyMapView.Loaded += MyMapView_Loaded;
        }

        private async void MyMapView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var info = await _printTask.GetTaskInfoAsync();
                
                comboLayout.ItemsSource = info.LayoutTemplates;
                if (info.LayoutTemplates != null && info.LayoutTemplates.Count > 0)
                    comboLayout.SelectedIndex = 0;

                comboFormat.ItemsSource = info.Formats;
                if (info.Formats != null && info.Formats.Count > 0)
                    comboFormat.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
        }

        private async void ExportMap_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                progress.Visibility = Visibility.Visible;

                PrintParameters printParameters = new PrintParameters(MyMapView)
                {
                    ExportOptions = new ExportOptions() { Dpi = 96, OutputSize = new Size(MyMapView.ActualWidth, MyMapView.ActualHeight) },
                    LayoutTemplate = (string)comboLayout.SelectedItem ?? string.Empty,
                    Format = (string)comboFormat.SelectedItem,
                };

                var result = await _printTask.PrintAsync(printParameters);

                Process.Start(result.Uri.AbsoluteUri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?