Tutorial: Advanced high-quality web map printing/exporting using arcpy.mapping
The illustration below shows a sample ArcGIS web API application that you will create. In the web application, the end user will be able to:
- Navigate to an area of interest.
- Select a staged layout template.
- Select an output format.
- Choose whether or not to export georeferencing information to an output PDF file by passing in an extra parameter from the web application to the Print Task. (More information on this below.)
- Control which service sublayers are on/off in the map.
- Control which service sublayers are on/off in the legend in the output file. (That is, a layer can be on in the map and off in the legend.)
- Export the map to a printer-friendly format containing high-quality vector output for service layers.
![Web application Web application](0155/GUID-9DD8BE54-BB42-4165-BD46-C77CC8F1E1C0-web.png)
By default, exporting from a web application using the Printing Tools service and its client print widgets in the ArcGIS web APIs outputs a cached image of the service layers, which may be at a resolution too low (for example, 96 dpi) for large-format printing or high-quality cartographic output. This tutorial will show you how to instead output a printer-friendly document containing high-quality vector output for service layers.
A sample output PDF is below. Note that layers that were turned off in the Table of Contents and Legend in the web application are reflected in the output.
![Output PDF Output PDF](0155/GUID-7504FAA6-C2C2-4372-83D3-06E8E383778C-web.png)
The code behind the Export Map button uses the ArcGIS web APIs' Print Task, which is available with ArcGIS 10.1 for Desktop. This tutorial will also show how to pass extra parameters into the Print Task, for example, whether or not to export georeferencing information to the output PDF file. The ability to pass extra parameters into the Print Task is useful as it allows you to collect any number of extra parameters from the web application. You will also create a Python script that will be published as a geoprocessing service that the Print Task will use. The Python script uses the ConvertWebMapToMapDocument function in the arcpy.mapping module which will insert the full state of the web map into a staged template map document. You will stage several templates from which the user can choose. Each template contains vector equivalents of all the possible layers in the map service. The staged templates can also contain other elements, such as a legend, dynamic text, and so on. The arcpy.mapping module also provides functionality to identify service layers and swap them out for layers that point to local data and to export to a variety of formats, for example, PDF.
To complete this tutorial, you should be familiar with the arcpy.mapping module, the ArcGIS web APIs, ArcGIS for Desktop, and ArcGIS 10.1 for Server. Sample code for all the ArcGIS web APIs is included in this tutorial. You should also become familiar with printing in the web application help topics:
This tutorial uses the MapTemplates and TemplateData folders in the ArcGIS for Desktop installation directory, usually located at C:\Program Files (x86)\ArcGIS\Desktop10.1. This tutorial assumes the map templates and template data are present and have not been modified. If this is not the case, you may need to reinstall ArcGIS for Desktop.
Copy the tutorial data
You will copy some of the the map templates in the ArcGIS for Desktop installation directory to a new folder. These will eventually be used as the staged map templates in the web application.
Before you do that, you need to make sure that a folder structure exists where ArcGIS for Server can see the template map documents and data that will be used in the web application. This tutorial assumes you have a folder that is registered with ArcGIS for Server. For more information on registering data with ArcGIS for Server, see:
![Tip Tip](rsrc/tip.png)
When using template map documents in the ConvertWebMapToMapDocument function, the best practice is to use data that is registered with ArcGIS for Server. If you choose to not use registered data, template map documents and data will be packaged and copied to the server. During packaging, data may be moved and re-sourced with relative paths to a folder structure that ConvertWebMapToMapDocument cannot resolve. For more information, see the ConvertWebMapToMapDocument help topic.
- Open a new, empty ArcMap session.
- In the Catalog window, navigate to your registered folder. Create a new folder in your registered folder called USA.
- In the Catalog window, navigate to the MapTemplates folder in the ArcGIS for Desktop installation directory. This is usually located at C:\Program Files (x86)\ArcGIS\Desktop10.1\MapTemplates. Further navigate down to the Traditional Layouts\USA folder.
- In the Catalog window, copy and paste the following files to the USA folder you created in a previous step: CentralUSA.mxd, ConterminousUSA.mxd, NortheasternUSA.mxd, NorthwesternUSA.mxd, SouthernUSA.mxd, and SouthwesternUSA.mxd.
- In the Catalog window, navigate to the TemplateData folder in the ArcGIS for Desktop installation directory. This is usually located at C:\Program Files (x86)\ArcGIS\Desktop10.1\TemplateData.
- In the Catalog window, copy and paste TemplateData.gdb to the USA folder you created in a previous step.
- The registered folder should look similar to this:
Note:
In the screen capture above, the registered folder is named MyDataStore. Your registered folder can be named anything.
Prepare the map documents to use as templates in the web application
The map documents need to be prepared for usage as templates in ConvertWebMapToMapDocument.
The map documents that are now in the registered folder need to be resourced to TemplateData.gdb, which is also in the registered folder. This can be accomplished through the ArcMap user interface or with a Python script. The latter approach will be used.
![Note Note](rsrc/note.png)
If you were authoring your own map document templates from scratch in the registered folder, these steps would not be required.
- Open the Python Window in ArcMap.
- Copy and paste the following script into the Python Window:
- In the Python Window, change the folderPath variable to be the path to the registered folder where the template MXDs reside. Remember to use the forward slash (/) in your path.
- In the Python Window, change the newPath variable to be the location of TemplateData.gdb within the registered folder. Remember to use the forward slash (/) in your path.
- The script should look like this:
- Position the cursor to the right of the last line. Press the ENTER key twice to execute the script. Wait for the script to execute.
import arcpy, os
# The path to the registered folder where the template MXDs reside
folderPath = "C:/MyDataStore/USA"
# The location of TemplateData.gdb within the registered folder
newPath = "C:/MyDataStore/USA/TemplateData.gdb"
# Loop through all MXDs in the specified folder and change the layer's data source to the new path
for filename in os.listdir(folderPath):
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath):
basename, extension = os.path.splitext(fullpath)
if extension.lower() == ".mxd":
mxd = arcpy.mapping.MapDocument(fullpath)
mxd.findAndReplaceWorkspacePaths(arcpy.mapping.ListLayers(mxd)[1].workspacePath, newPath)
mxd.save()
print "done"
The map documents are now ready to be used as templates in the web application.
Create a map service to be used in the web application
A map document needs to be prepared and published as the map service to be used in the web application. ConterminousUSA.mxd in the registered folder will be used as the map service. Before you publish it to your server, you need to prepare it for publishing.
- Open ConterminousUSA.mxd in ArcMap.
- By default, the map will open up in layout view. Switch to data view by selecting View > Data View in the ArcMap main menu.
- You will now turn off the Map Annotation Group in this map document. This map contains map graphics. Map graphics increase the memory footprint of a loaded document,
which leads to performance degradation. Due to these side effects,
map graphics are not supported in map services.
- Open the Conterminous United States data frame properties by right-clicking the data frame's name in the table of contents and clicking Properties.
- Click the Annotation Groups tab.
- Uncheck the <Default> group. The Annotation Groups tab should now look like this:
- Click OK on the Data Frame Properties dialog box.
- This map uses basemap layers. Basemap layers will not publish to ArcGIS for Server. You will move the layers out of the basemap layer by ungrouping it.
- Right-click the Basemap layer in the table of contents and click Ungroup.
Note:
If you were authoring your own map document templates from scratch or were using existing map document templates that did not contain map graphics or basemap layers, these steps would not be required.
The map is now ready to be published to your server. If you are unfamiliar with the publishing process, please review how to publish a service.
- Click File > Share As > Service.
- Click Publish a service.
- Click Next.
- Choose a publish or admin connection to your ArcGIS 10.1 for Server machine.
- Click Next.
- Create a new folder called USA.
- Click Continue.
- In the top right corner of the Service Editor dialog box, click Publish.
This will create the map service that will be used in the web application.
Create the Python script
You will create a Python script that will be used as the custom geoprocessing service.
The Python script in the custom geoprocessing service executes the ConvertWebMapToMapDocument function which converts a web map (in JSON format) that you intend to print or export to a map document. The arcpy.mapping script then loops through all the layers in the output map document, removing all layers except the vector layers that correspond to the service layers in the web map JSON. It then loops through all the layers in the legend, removing all legend layers except the vector layers that correspond to the service layers in the web map JSON legend. The script will also read extra parameters from the custom print task, in this case, whether or not to export georeferencing information to the output PDF file. The map document can then be exported to the format of your choice, for example, PDF.
- Open any Python IDE, such as IDLE (which comes with ArcGIS for Desktop).
- Copy and paste the following code into a new Python script.
- Change the templatePath variable to be the UNC path to the folder in your registered folder which contains the template map documents.
- Save the Python script. Name the script AdvancedHighQualityPrinting.py. Save it in a folder called WebApp within the registered folder.
import arcpy
import os
import uuid
# The template location in the registered folder (as UNC path)
templatePath = '//MyComputerName/MyDataStore/USA'
# Input WebMap json
Web_Map_as_JSON = arcpy.GetParameterAsText(0)
# Format for output
Format = arcpy.GetParameterAsText(1)
if Format == '#' or not Format:
Format = "PDF"
# Input Layout template
Layout_Template = arcpy.GetParameterAsText(2)
if Layout_Template == '#' or not Layout_Template:
Layout_Template = "NorthwesternUSA"
# Extra parameter - georef_info
Georef_info = arcpy.GetParameterAsText(3)
if Georef_info == '#' or not Georef_info:
Georef_info = "False"
# Convert Georef_info string to boolean
if Georef_info.lower() == 'false':
Georef_info_bol = False
elif Georef_info.lower() == 'true':
Georef_info_bol = True
# Get the requested map document
templateMxd = os.path.join(templatePath, Layout_Template + '.mxd')
# Convert the WebMap to a map document
result = arcpy.mapping.ConvertWebMapToMapDocument(Web_Map_as_JSON, templateMxd)
mxd = result.mapDocument
# Reference the data frame that contains the webmap
# Note: ConvertWebMapToMapDocument renames the active dataframe in the template_mxd to "Webmap"
df = arcpy.mapping.ListDataFrames(mxd, 'Webmap')[0]
# Get a list of all service layer names in the map
serviceLayersNames = [slyr.name for slyr in arcpy.mapping.ListLayers(mxd, data_frame=df)
if slyr.isServiceLayer and slyr.visible and not slyr.isGroupLayer]
# Create a list of all possible vector layer names in the map that could have a
# corresponding service layer
vectorLayersNames = [vlyr.name for vlyr in arcpy.mapping.ListLayers(mxd, data_frame=df)
if not vlyr.isServiceLayer and not vlyr.isGroupLayer]
# Get a list of all vector layers that don't have a corresponding service layer
removeLayerNameList = [vlyrName for vlyrName in vectorLayersNames
if vlyrName not in serviceLayersNames]
# Remove all vector layers that don't have a corresponding service layer
for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
if not lyr.isGroupLayer \
and not lyr.isServiceLayer \
and lyr.name in removeLayerNameList \
and lyr.name in vectorLayersNames:
arcpy.mapping.RemoveLayer(df, lyr)
# Reference the legend in the map document
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
# Get a list of service layers that are on in the legend because the incoming
# JSON can specify which service layers/sublayers are on/off in the legend
legendServiceLayerNames = [lslyr.name for lslyr in legend.listLegendItemLayers()
if lslyr.isServiceLayer and not lslyr.isGroupLayer]
# Remove vector layers from the legend where the corresponding service layer
# is also off in the legend
for lvlyr in legend.listLegendItemLayers():
if not lvlyr.isServiceLayer \
and lvlyr.name not in legendServiceLayerNames \
and not lvlyr.isGroupLayer \
and lvlyr.name in vectorLayersNames:
legend.removeItem(lvlyr)
# Remove all service layers
# This will leave only vector layers that had corresponding service layers
for slyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
if slyr.isServiceLayer:
arcpy.mapping.RemoveLayer(df, slyr)
# ConvertWebMapToMapDocument renames the active dataframe in the template_mxd to "Webmap".
# Lets rename it to something more meaningful.
df.name = Layout_Template
# Use the uuid module to generate a GUID as part of the output name
# This will ensure a unique output name
output = 'WebMap_{}.{}'.format(str(uuid.uuid1()), Format)
Output_File = os.path.join(arcpy.env.scratchFolder, output)
# Export the WebMap
if Format.lower() == 'pdf':
arcpy.mapping.ExportToPDF(mxd, Output_File, georef_info=Georef_info_bol)
elif Format.lower() == 'png':
arcpy.mapping.ExportToPNG(mxd, Output_File)
# Set the output parameter to be the output file of the server job
arcpy.SetParameterAsText(4, Output_File)
# Clean up - delete the map document reference
filePath = mxd.filePath
del mxd, result
os.remove(filePath)
![Note Note](rsrc/note.png)
If the ArcGIS for Server, ArcGIS for Desktop, and the registered folder are all the same machine, then UNC paths to the registered folder are not required. Instead, absolute paths can be used.
The Python script is now ready to be added to a toolbox.
Create the toolbox
- In the Catalog window in ArcMap, navigate to the WebApp folder in the registered folder.
- Right-click the WebApp folder and click New > Toolbox. Name the toolbox AdvancedHighQualityPrinting.
- Right-click the AdvancedHighQualityPrinting toolbox and click Item Description.
- In the Item Description dialog box, populate in the Tags and Summary items with the text of your choice. Optionally, fill in other item descriptions.
- Click Save and exit the Item Description dialog box.
Create a Python script tool
Next, you will add the AdvancedHighQualityPrinting.py script to the AdvancedHighQualityPrinting toolbox.
- In the Catalog window, right-click the AdvancedHighQualityPrinting toolbox and click Add > Script.
- In the Add Script dialog box, enter AdvancedHighQualityPrinting for both the Name and the Label.
- Click Next.
- For the Script File, navigate to the WebApp folder in your registered folder and select AdvancedHighQualityPrinting.py.
- Click Next in the Add Script dialog box.
- Five parameters need to be added to the script tool.
- The first parameter will be Web_Map_as_JSON. This parameter takes in a JSON representation of the state of the map to be exported as it
appears in the web application.
The properties should match the following screen capture:
- The next parameter will be Format—the format in which the map image for printing will be delivered.
The properties should match the following screen capture:
- The next parameter will be Layout_Template—the template map document that will be used. The properties should match the following screen capture:
- The next parameter will be Georef_info—a string Boolean that enables the export of coordinate system information into the output PDF file. The properties should match the following screen capture:
- The next parameter will be Output_File—the output file that will be created. The properties should match the following screen capture:
Caution:
The Web_Map_as_JSON, Format, Layout_Template, and Output_File parameter names must be spelled exactly as shown so they match the tool signature of the Print Task in the ArcGIS web APIs. For more information on the parameters, see Export Web Map. The Georef_info parameter is an extra parameter that will be passed to the Print Task and therefore has no naming convention to conform to.
- Click Finish on the Add Script dialog box.
- The first parameter will be Web_Map_as_JSON. This parameter takes in a JSON representation of the state of the map to be exported as it
appears in the web application.
The properties should match the following screen capture:
- Right-click the AdvancedHighQualityPrinting script tool and click Item Description.
- In the Item Description dialog box, populate the Tags and Summary items with text of your choice. Also populate the Dialog Explanation for all five parameters in the Syntax section of the Item Description dialog box. Optionally, fill in other item descriptions.
Execute the tool
The tool needs to be executed successfully to create a result in the Results window that can be published to ArcGIS for Server. This illustrates an interesting problem with testing scripts that use ConvertWebMapToMapDocument. In order to execute locally, it needs a valid webmap JSON. However, the ArcGIS web APIs provide the webmap JSON in the web application, but only after the script has been published. To get around this, you can use the test JSON that is in the ConvertWebMapToMapDocument help topic. Moreover, you can modify the JSON to contain elements that the tool is expecting and that will be similar to the JSON you will be getting from the web application. For this tutorial, the web application user will be turning service layers on/off in the map, as well as further turning layers on/off in the legend. Therefore, a test web map JSON string like this should be used:
{
"operationalLayers": [
{
"url": "http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer",
"title": "MyDynamicService",
"opacity": 1,
"id": "MyDynamicService",
"visibility": true,
"visibleLayers": [
0,
2,
3,
4,
5,
6,
7
]
}
],
"mapOptions": {
"extent": {
"xmin": -2200000,
"ymin": 500000,
"xmax": -1300000,
"ymax": 1300000,
"spatialReference": {
"wkid": 102008
}
},
"scale": 5000000,
"rotation": 0
},
"layoutOptions": {
"copyrightText": "",
"authorText": "",
"legendOptions": {
"operationalLayers": [
{
"id": "MyDynamicService",
"subLayerIds": [
0,
2,
5
]
}
]
}
}
}
Note that the visibleLayers element has layer 1 off. This means that layer one in the map service will not be present in the output. Also note the subLayerIDs in the legendOptions. Only layers 0, 2, and 5 will be on in the legend. This is similar to the JSON that will be returned from the web application. For more information, see the WebMap JSON Specifications.
When running the script tool, the JSON string can be copied and pasted into the Web_Map_as_JSON input parameter. However, the line breaks must be removed in order for the string to be valid input. The JSON string with line breaks removed is below:
{"operationalLayers":[{"url":"http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer","title":"MyDynamicService","opacity":1,"id":"MyDynamicService","visibility":true,"visibleLayers":[0,2,3,4,5,6,7]}],"mapOptions":{"extent":{"xmin":-2200000,"ymin":500000,"xmax":-1300000,"ymax":1300000,"spatialReference":{"wkid":102008}},"scale":5000000,"rotation":0},"layoutOptions":{"copyrightText":"","authorText":"","legendOptions":{"operationalLayers":[{"id":"MyDynamicService","subLayerIds":[0,2,5]}]}}}
You also need to change the name of the server (MyServer:6080, above) to that of your ArcGIS for Server server.
![Tip Tip](rsrc/tip.png)
For publishing purposes, you can choose to leave the Web_Map_as_JSON input parameter blank as the ArcGIS web APIs will provide the web map JSON in the web application. You can leave the Web_Map_as_JSON input parameter blank provided the Python script was written in such a way as to not fail with blank input. For example, the script doesn't look for web map layers by name. If the script fails on the server due to an error in the script, it will have to be fixed locally, then republished to the server. Therefore, it is good practice to ensure that the script works locally before publishing by testing with a valid web map JSON string or using a debug map document that contains all the elements that would be in the web map JSON. For this tutorial we will provide a valid JSON string.
- In the Catalog window, right-click the AdvancedHighQualityPrinting script tool and click Open.
- For the Web_Map_as_JSON input parameter, copy and paste the JSON string with line breaks removed from the single line code block above to the Web_Map_as_JSON parameter. Don't forget to change the name of the server to match your server. The AdvancedHighQualityPrinting script tool dialog box should look this:
- Accept all the default values for the other parameters and click OK.
- Open the Results window.
- Double-click Output_File, highlighted below, to see the output PDF.
This will open the PDF. As specified in the web map JSON, the PDF should have layer one turned off in the map; and only layers 0, 2, and 5 should be on in the legend.
Publish the result
The result is now ready to be published as a geoprocessing service. If you are unfamiliar with publishing geoprocessing services, see:
- In the Results window, right-click the AdvancedHighQualityPrinting result and click Share As > Geoprocessing Service.
- Click Publish a service.
- Click Next.
- Choose a publish or admin connection to your ArcGIS 10.1 for Server machine.
- Click Next.
- Use the existing folder called USA.
- Click Continue.
- In the top right corner of the Service Editor dialog box, click Publish.
- After the script tool successfully publishes, it is a good practice to test the geoprocessing service in ArcMap using a GIS server connection to your ArcGIS for Server. You can see the result of the geoprocessing service in the Geoprocessing Results window.
The geoprocessing service is now ready to be used in the ArcGIS web APIs.
Choose which ArcGIS web API you want to use to create the web application
The ArcGIS web APIs offer similar GIS functionality; it is up to you to choose your preferred developer platform. Each API provides the resources you need. Read the API reference; view working samples that help you get started and show you what's possible; and communicate with others through the forums, blogs, and code galleries. It is beyond the scope of this tutorial to go into detail about getting started building web applications with each API. Please consult the API documentation for that. For more information, see:
Each ArcGIS web API has a Print Task. The Print Task has a URL property which will reference the custom geoprocessing service that you created earlier. Please consult the ArcGIS web API documentation for more information on the Print Task.
In the sections below, use the code samples for the ArcGIS web API that you work in to build your web application.
ArcGIS API for Silverlight sample code
If you are using the ArcGIS API for Silverlight, use the following sample code to build your web application.
In the ArcGIS API for Silverlight code samples below, change the URL to the map service and geoprocessing service that you created in the previous steps to match your server name. They are referenced on these lines:
Url="http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer"
printTask = new PrintTask("http://MyServer:6080/arcgis/rest/services/USA/AdvancedHighQualityPrinting/GPServer/AdvancedHighQualityPrinting");
XAML code:
<UserControl x:Class="TestSilverlightApplication1.MainPage"
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 x:Name="MyMap" Extent="-2260000,-1500000,2250000,1220000">
<esri:Map.Layers>
<esri:ArcGISDynamicMapServiceLayer ID="MyDynamicService"
Url="http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer"
Initialized="ArcGISDynamicMapServiceLayer_Initialized"/>
</esri:Map.Layers>
</esri:Map>
<StackPanel Orientation="Vertical" Margin="15" HorizontalAlignment="Left" VerticalAlignment="Top" >
<Border x:Name="PrintPanel" CornerRadius="10" 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/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="LayoutTemplates" Grid.Row="0" Grid.Column="0" Margin="2"/>
<ComboBox x:Name="LayoutTemplates" ItemsSource="{Binding LayoutTemplates}" Grid.Row="0" Grid.Column="1" Width="100" Margin="2"/>
<TextBlock Text="Formats" Grid.Row="1" Grid.Column="0" Margin="2"/>
<ComboBox x:Name="Formats" Grid.Row="1" ItemsSource="{Binding Formats}" Grid.Column="1" Width="100" Margin="2"/>
<TextBlock Text="Georef info?" Grid.Row="2" Grid.Column="0" Margin="2"/>
<ComboBox x:Name="Georef_info" Grid.Row="2" Grid.Column="1" Width="100" Margin="2">
<ComboBoxItem Content="True"></ComboBoxItem>
<ComboBoxItem Content="False"></ComboBoxItem>
</ComboBox>
<Button x:Name="Print" Content="Export Map" Click="ExportMap_Click"
Grid.Row="3" Grid.Column="0" Margin="2"/>
</Grid>
</Border>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="15" HorizontalAlignment="Left" VerticalAlignment="Center" >
<Border Background="#99919191" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="20,20,20,30" Padding="10" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Table of Contents" Foreground="White" Grid.Row="0" >
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="1" />
</TextBlock.Effect>
</TextBlock>
<ListBox Name="TOCListBox" Margin="0,5,0,0" ItemsSource="{Binding ElementName=MyMap, Path=Layers.[MyDynamicService].Layers}"
Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Margin="2"
Name="MyDynamicService"
Content="{Binding Name}"
IsChecked="{Binding DefaultVisibility}"
Tag="{Binding ID}"
ClickMode="Press"
Click="CheckBox_Click" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="15" HorizontalAlignment="Left" VerticalAlignment="Bottom" >
<Border Background="#99919191" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="20,20,20,30" Padding="10" BorderBrush="Black">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="18" />
<ColumnDefinition Width="62" />
<ColumnDefinition Width="6" />
<ColumnDefinition Width="56" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="15" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Include in Legend" Foreground="White" Grid.ColumnSpan="4">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="1" />
</TextBlock.Effect>
</TextBlock>
<ListBox Name="LegendListBox" Margin="0,5,0,0" Grid.Row="1" Grid.ColumnSpan="4">
<ListBoxItem>
<CheckBox x:Name="CapitalCities" Content="Capital Cities" IsChecked="True" ClickMode="Press"></CheckBox>
</ListBoxItem>
<ListBoxItem>
<CheckBox x:Name="InterstateHighways" Content="Interstate Highways" IsChecked="True" ClickMode="Press"></CheckBox>
</ListBoxItem>
<ListBoxItem>
<CheckBox x:Name="Rivers" Content="Rivers" IsChecked="True" ClickMode="Press"></CheckBox>
</ListBoxItem>
<ListBoxItem>
<CheckBox x:Name="Lakes" Content="Lakes" IsChecked="True" ClickMode="Press"></CheckBox>
</ListBoxItem>
<ListBoxItem>
<CheckBox x:Name="StateBoundaries" Content="State Boundaries" IsChecked="True" ClickMode="Press"></CheckBox>
</ListBoxItem>
</ListBox>
</Grid>
</Border>
</StackPanel>
</Grid>
</UserControl>
Code behind in C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Printing;
using ESRI.ArcGIS.Client.Tasks;
namespace TestSilverlightApplication1
{
public partial class MainPage : UserControl
{
PrintTask printTask;
public MainPage()
{
InitializeComponent();
printTask = new PrintTask("http://MyServer:6080/arcgis/rest/services/USA/AdvancedHighQualityPrinting/GPServer/AdvancedHighQualityPrinting");
printTask.DisableClientCaching = true;
printTask.JobCompleted += new EventHandler<PrintJobEventArgs>(printTask_JobCompleted);
printTask.GetServiceInfoCompleted += new EventHandler<ServiceInfoEventArgs>(printTask_GetServiceInfoCompleted);
printTask.GetServiceInfoAsync();
}
void printTask_GetServiceInfoCompleted(object sender, ServiceInfoEventArgs e)
{
PrintPanel.DataContext = e.ServiceInfo;
}
void printTask_JobCompleted(object sender, PrintJobEventArgs e)
{
if (e.Error == null)
{
if (e.PrintResult != null)
System.Windows.Browser.HtmlPage.Window.Navigate(e.PrintResult.Url, "_blank");
}
}
private void ExportMap_Click(object sender, RoutedEventArgs e)
{
if (printTask == null || printTask.IsBusy) return;
ComboBoxItem typeItem = (ComboBoxItem)Georef_info.SelectedItem;
string value = typeItem.Content.ToString();
var gpExtraInput = new List<GPParameter>();
gpExtraInput.Add(new GPString("Georef_info", value));
// Specify the LayoutOptions to provide marginalia in the output map.
ESRI.ArcGIS.Client.Printing.LayoutOptions myLayoutOptions = new ESRI.ArcGIS.Client.Printing.LayoutOptions();
// Define the LegendOptions. This will show specified Layers in the printed map's legend area.
ESRI.ArcGIS.Client.Printing.LegendOptions myLegendOptions = new ESRI.ArcGIS.Client.Printing.LegendOptions();
// Define a List<LegendInfo> objects to put in the LegendOptions.
List<ESRI.ArcGIS.Client.Printing.LegendInfo> myLegendInfos = new List<ESRI.ArcGIS.Client.Printing.LegendInfo>();
// Loop through all of the Layers in the Map Control.
foreach (var myLayer in MyMap.Layers)
{
// Create a new LegendInfo object to put in the List<LegendInfo> collection.
ESRI.ArcGIS.Client.Printing.LegendInfo myLegendInfo = new ESRI.ArcGIS.Client.Printing.LegendInfo();
// Set the LegendInfo.LayerId to the Layer.ID to add it to the printed map's legend area.
myLegendInfo.LayerId = myLayer.ID;
//TODO: Add SubLayer IDs to LegendInfo
var l = MyMap.Layers["MyDynamicService"] as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
List<int> legendLayerIntList = new List<int>();
foreach (var s in l.Layers)
{
if (s.ID == 0)
{
if (CapitalCities.IsChecked == true)
{
legendLayerIntList.Add(s.ID);
}
}
else if (s.ID == 1)
{
if (InterstateHighways.IsChecked == true)
{
legendLayerIntList.Add(s.ID);
}
}
else if (s.ID == 2)
{
if (Rivers.IsChecked == true)
{
legendLayerIntList.Add(s.ID);
}
}
else if (s.ID == 3)
{
if (Lakes.IsChecked == true)
{
legendLayerIntList.Add(s.ID);
}
}
else if (s.ID == 4)
{
if (StateBoundaries.IsChecked == true)
{
legendLayerIntList.Add(s.ID);
}
}
}
myLegendInfo.SubLayerIds = from x in legendLayerIntList
select x as object;
// Add a single LegendInfo into the List<LegendInfo> collection.
myLegendInfos.Add(myLegendInfo);
}
// Set the LegendOptions.LegendInfo to the new constructed List<LegendInfo> objects.
myLegendOptions.LegendInfos = myLegendInfos;
// Set the LayoutOptions.LegendOptions to manage what the Legend looks like in the output map.
myLayoutOptions.LegendOptions = myLegendOptions;
PrintParameters printParameters = new PrintParameters(MyMap)
{
LayoutTemplate = (string)LayoutTemplates.SelectedItem,
Format = (string)Formats.SelectedItem,
CustomInputParameters = gpExtraInput
};
printParameters.LayoutOptions = myLayoutOptions;
printTask.SubmitJobAsync(printParameters);
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox tickedCheckBox = sender as CheckBox;
string serviceName = tickedCheckBox.Name;
bool visible = (bool)tickedCheckBox.IsChecked;
int layerIndex = (int)tickedCheckBox.Tag;
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
List<int> visibleLayerList =
dynamicServiceLayer.VisibleLayers != null
? dynamicServiceLayer.VisibleLayers.ToList() : new List<int>();
if (visible)
{
if (!visibleLayerList.Contains(layerIndex))
visibleLayerList.Add(layerIndex);
}
else
{
if (visibleLayerList.Contains(layerIndex))
visibleLayerList.Remove(layerIndex);
}
dynamicServiceLayer.VisibleLayers = visibleLayerList.ToArray();
}
public void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
{
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer =
sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
if (dynamicServiceLayer.VisibleLayers == null)
dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer);
}
private int[] GetDefaultVisibleLayers(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicService)
{
List<int> visibleLayerIDList = new List<int>();
ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray = dynamicService.Layers;
for (int index = 0; index < layerInfoArray.Length; index++)
{
if (layerInfoArray[index].DefaultVisibility)
visibleLayerIDList.Add(index);
}
return visibleLayerIDList.ToArray();
}
}
}
ArcGIS API for Flex sample code
If you are using the ArcGIS API for Flex, use the following sample code to build your web application.
In the ArcGIS API for Flex code samples below, change the URL to the map service and geoprocessing service that you created in the previous steps to match your server name. They are referenced on these lines:
url="http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer"/>
url="http://MyServer:6080/arcgis/rest/services/USA/AdvancedHighQualityPrinting/GPServer/AdvancedHighQualityPrinting"/>
Code for AdvancedHighQualityPrinting.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:esri="http://www.esri.com/2008/ags"
initialize="printTask.getServiceInfo()"
pageTitle="High-quality printing"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:samples="IncludeFiles.*">
<!--
This sample also uses the following files:
IncludeFiles/LayerTOC.mxml - a datagrid with checkboxes for every layer in the map service
IncludeFiles/LayerVizRenderer.mxml - The datagrid itemrenderer (LayerVizRenderer) will update the map service as users selects/clears checkboxes.
-->
<fx:Script>
<![CDATA[
import com.esri.ags.events.PrintEvent;
import com.esri.ags.tasks.supportClasses.DataFile;
import com.esri.ags.tasks.supportClasses.JobInfo;
import com.esri.ags.tasks.supportClasses.ParameterValue;
import mx.controls.Alert;
private function printBtn_clickHandler(event:MouseEvent):void
{
myLegendLayer.subLayerIds = [];
if (CapitalCities.selected)
{
myLegendLayer.subLayerIds.push(0);
}
if (InterstateHighways.selected)
{
myLegendLayer.subLayerIds.push(1);
}
if (Rivers.selected)
{
myLegendLayer.subLayerIds.push(2);
}
if (Lakes.selected)
{
myLegendLayer.subLayerIds.push(3);
}
if (StateBoundaries.selected)
{
myLegendLayer.subLayerIds.push(4);
}
if (printTask.getServiceInfoLastResult.isServiceAsynchronous)
{
printTask.submitJob(printParameters);
}
else
{
printTask.execute(printParameters);
}
}
private function printTask_jobCompleteHandler(event:PrintEvent):void
{
var jobInfo:JobInfo = event.jobInfo;
if (jobInfo.jobStatus == JobInfo.STATUS_SUCCEEDED)
{
printTask.getResultData(jobInfo.jobId);
}
else
{
Alert.show(jobInfo.jobStatus);
}
}
private function printTask_getResultDataCompleteHandler(event:PrintEvent):void
{
var dataFile:DataFile = event.parameterValue.value as DataFile;
navigateToURL(new URLRequest(dataFile.url));
}
private function printTask_executeCompleteHandler(event:PrintEvent):void
{
var paramValue:ParameterValue = event.executeResult.results[0];
var dataFile:DataFile = paramValue.value as DataFile;
navigateToURL(new URLRequest(dataFile.url));
}
]]>
</fx:Script>
<fx:Declarations>
<esri:PrintTask id="printTask"
executeComplete="printTask_executeCompleteHandler(event)"
fault="Alert.show(event.fault.faultString)"
getResultDataComplete="printTask_getResultDataCompleteHandler(event)"
jobComplete="printTask_jobCompleteHandler(event)"
showBusyCursor="true"
url="http://MyServer:6080/arcgis/rest/services/USA/AdvancedHighQualityPrinting/GPServer/AdvancedHighQualityPrinting"/>
<esri:PrintParameters id="printParameters"
format="{formats.selectedItem}"
layoutTemplate="{layoutTemplates.selectedItem}"
map="{map}">
<esri:customParameters>
<fx:Object Georef_info="{Georef_info.selectedItem}"/>
</esri:customParameters>
<esri:layoutOptions>
<esri:LayoutOptions title="My Map">
<esri:legendOptions>
<esri:LegendOptions>
<esri:LegendLayer id="myLegendLayer" layerId="myDynamicService" />
</esri:LegendOptions>
</esri:legendOptions>
</esri:LayoutOptions>
</esri:layoutOptions>
</esri:PrintParameters>
</fx:Declarations>
<s:controlBarLayout>
<s:HorizontalLayout gap="10"
paddingBottom="7"
paddingLeft="10"
paddingRight="10"
paddingTop="7"
verticalAlign="baseline"/>
</s:controlBarLayout>
<s:controlBarContent>
<s:Label text="Layout Templates"/>
<s:DropDownList id="layoutTemplates"
width="175"
dataProvider="{printTask.getServiceInfoLastResult.layoutTemplates}"/>
<s:Label text="Formats"/>
<s:DropDownList id="formats"
width="100"
dataProvider="{printTask.getServiceInfoLastResult.formats}"/>
<s:Label text="Georef info?"/>
<s:DropDownList id="Georef_info"
width="100">
<s:dataProvider>
<s:ArrayList source="[True,False]"/>
</s:dataProvider>
</s:DropDownList>
<s:Button id="printBtn"
click="printBtn_clickHandler(event)"
enabled="{printTask.getServiceInfoLastResult != null}"
label="Export Map"/>
</s:controlBarContent>
<mx:HDividedBox x="164" width="867" height="100%">
<esri:Map id="map" width="632" level="2" wrapAround180="true">
<esri:ArcGISDynamicMapServiceLayer id="myDynamicService"
url="http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer"/>
<esri:extent>
<esri:Extent id="myExtent" xmin="-2260000" ymin="-1500000" xmax="2250000" ymax="1220000"/>
</esri:extent>
</esri:Map>
<samples:LayerTOC width="210" height="493"
mapLayer="{myDynamicService}"/>
<s:VGroup>
</s:VGroup>
</mx:HDividedBox>
<s:CheckBox id="CapitalCities" x="21" y="30" label="Capital Cities" selected="true"/>
<s:CheckBox id="InterstateHighways" x="21" y="56" label="Interstate Highways" selected="true"/>
<s:Label x="21" y="10" text="Include in Legend?"/>
<s:CheckBox id="Rivers" x="21" y="82" label="Rivers" selected="true"/>
<s:CheckBox id="Lakes" x="21" y="108" label="Lakes" selected="true"/>
<s:CheckBox id="StateBoundaries" x="21" y="134" label="State Boundaries" selected="true"/>
</s:Application>
Code for LayerTOC.mxml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Used by High Quality Printing mxml -->
<mx:DataGrid xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%"
resizableColumns="false"
sortableColumns="false">
<fx:Script>
<![CDATA[
import com.esri.ags.events.LayerEvent;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
import com.esri.ags.layers.ArcIMSMapServiceLayer;
import com.esri.ags.layers.Layer;
import com.esri.ags.layers.supportClasses.LayerInfo;
import mx.collections.ArrayList;
import mx.collections.IList;
import mx.utils.ObjectUtil;
private var _layerInfos:Array;
private var _layer:Layer;
private var _layerChanged:Boolean;
public function get mapLayer():Layer
{
return _layer;
}
public function set mapLayer(value:Layer):void
{
_layer = value;
_layerChanged = true;
invalidateProperties();
}
override protected function commitProperties():void
{
if (_layerChanged)
{
_layerChanged = false;
if (mapLayer)
{
mapLayer.addEventListener(LayerEvent.UPDATE_END, removeBusyCursor, false, 0, true);
if (mapLayer.loaded)
{
setDataProvider();
}
else
{
mapLayer.addEventListener(LayerEvent.LOAD, layerLoadHandler, false, 0, true);
}
}
}
super.commitProperties();
}
private function setDataProvider():void
{
if (mapLayer is ArcGISDynamicMapServiceLayer)
{
_layerInfos = ArcGISDynamicMapServiceLayer(mapLayer).layerInfos;
}
else if (mapLayer is ArcIMSMapServiceLayer)
{
_layerInfos = ArcIMSMapServiceLayer(mapLayer).layerInfos;
}
if (_layerInfos)
{
// make sure copy has typed LayerInfo objects
registerClassAlias("com.esri.ags.layers.supportClasses.LayerInfo", LayerInfo);
// create a copy since it'll be modified
_layerInfos = ObjectUtil.copy(_layerInfos) as Array;
// remove group layers and correct defaultVisibility
if (mapLayer is ArcGISDynamicMapServiceLayer)
{
var visibleLayers:Array = getDefaultVisibleLayers();
var layerInfos:Array = _layerInfos.concat(); // create a shallow clone
_layerInfos = [];
for each (var layerInfo:LayerInfo in layerInfos)
{
if (!layerInfo.subLayerIds) // skip group layers
{
layerInfo.defaultVisibility = visibleLayers.indexOf(layerInfo.layerId) != -1;
_layerInfos.push(layerInfo);
}
}
}
dataProvider = _layerInfos;
}
}
private function getDefaultVisibleLayers():Array
{
var result:Array = [];
for each (var layerInfo:LayerInfo in _layerInfos)
{
if (layerInfo.parentLayerId >= 0 && result.indexOf(layerInfo.parentLayerId) == -1)
{
// layer is not visible if it's parent is not visible
continue;
}
if (layerInfo.defaultVisibility)
{
result.push(layerInfo.layerId);
}
}
return result;
}
private function layerLoadHandler(event:LayerEvent):void
{
setDataProvider();
}
public function showLayer(layerInfo:LayerInfo):void
{
var visibleLayers:IList;
if (mapLayer is ArcGISDynamicMapServiceLayer)
{
var dynamicLayer:ArcGISDynamicMapServiceLayer = mapLayer as ArcGISDynamicMapServiceLayer;
visibleLayers = dynamicLayer.visibleLayers;
if (visibleLayers)
{
visibleLayers.addItem(layerInfo.layerId); // add id
}
else
{
visibleLayers = dynamicLayer.visibleLayers = getDynamicVisibleLayers();
}
}
else if (mapLayer is ArcIMSMapServiceLayer)
{
var arcIMSLayer:ArcIMSMapServiceLayer = mapLayer as ArcIMSMapServiceLayer;
visibleLayers = arcIMSLayer.visibleLayers;
if (visibleLayers)
{
visibleLayers.addItem(layerInfo.name); // add name
}
else
{
visibleLayers = arcIMSLayer.visibleLayers = getArcIMSVisibleLayers();
}
}
if (visibleLayers)
{
cursorManager.setBusyCursor();
}
}
public function hideLayer(layerInfo:LayerInfo):void
{
var visibleLayers:IList;
if (mapLayer is ArcGISDynamicMapServiceLayer)
{
var dynamicLayer:ArcGISDynamicMapServiceLayer = mapLayer as ArcGISDynamicMapServiceLayer;
visibleLayers = dynamicLayer.visibleLayers;
if (visibleLayers)
{
var idIndex:int = visibleLayers.getItemIndex(layerInfo.layerId);
if (idIndex != -1)
{
visibleLayers.removeItemAt(idIndex);
}
}
else
{
visibleLayers = dynamicLayer.visibleLayers = getDynamicVisibleLayers();
}
}
else if (mapLayer is ArcIMSMapServiceLayer)
{
var arcIMSLayer:ArcIMSMapServiceLayer = mapLayer as ArcIMSMapServiceLayer;
visibleLayers = arcIMSLayer.visibleLayers;
if (visibleLayers)
{
var nameIndex:int = visibleLayers.getItemIndex(layerInfo.name);
if (nameIndex != -1)
{
visibleLayers.removeItemAt(nameIndex);
}
}
else
{
visibleLayers = arcIMSLayer.visibleLayers = getArcIMSVisibleLayers();
}
}
if (visibleLayers)
{
cursorManager.setBusyCursor();
}
}
private function getDynamicVisibleLayers():IList
{
var result:ArrayList = new ArrayList();
for each (var layerInfo:LayerInfo in _layerInfos)
{
if (layerInfo.defaultVisibility)
{
result.addItem(layerInfo.layerId);
}
}
return result;
}
private function getArcIMSVisibleLayers():IList
{
var result:ArrayList = new ArrayList();
for each (var layerInfo:LayerInfo in _layerInfos)
{
if (layerInfo.defaultVisibility)
{
result.addItem(layerInfo.name);
}
}
return result;
}
private function removeBusyCursor(event:Event):void
{
cursorManager.removeBusyCursor();
}
]]>
</fx:Script>
<mx:columns>
<mx:DataGridColumn width="70"
headerText="Visibility"
itemRenderer="IncludeFiles.LayerVizRenderer"/>
<mx:DataGridColumn dataField="name" headerText="Layer Name"/>
</mx:columns>
</mx:DataGrid>
Code for LayerVizRenderer.mxml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Used by LayerTOC.mxml -->
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import com.esri.ags.layers.supportClasses.LayerInfo;
private function cb_clickHandler(event:MouseEvent):void
{
var layerInfo:LayerInfo = LayerInfo(data);
if (cb.selected)
{
layerInfo.defaultVisibility = true;
LayerTOC(dataGridListData.owner).showLayer(layerInfo);
}
else
{
layerInfo.defaultVisibility = false;
LayerTOC(dataGridListData.owner).hideLayer(layerInfo);
}
}
]]>
</fx:Script>
<s:CheckBox id="cb"
click="cb_clickHandler(event)"
horizontalCenter="0"
selected="{LayerInfo(data).defaultVisibility}"/>
</s:MXDataGridItemRenderer>
ArcGIS API for JavaScript sample code
If you are using the ArcGIS API for JavaScript, use the following sample code to build your web application.
In the ArcGIS API for JavaScript code sample below, change the URL to the map service and geoprocessing service that you created in the previous steps to match your server name. They are referenced on these lines:
var dynUrl = "http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer";
var printUrl = "http://MyServer:6080/arcgis/rest/services/USA/AdvancedHighQualityPrinting/GPServer/AdvancedHighQualityPrinting";
Code for AdvancedHighQualityPrinting.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dojo/dijit/themes/claro/claro.css">
<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.0/"></script>
<script type="text/javascript" language="Javascript">
dojo.require("esri.map");
dojo.require("esri.tasks.PrintTask");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.CheckBox");
dojo.require("dijit.form.ComboBox");
var layer, map, visible = [];
var printTask, params;
function init() {
var startExtent = new esri.geometry.Extent({"xmin":-2260000,"ymin":-1500000,"xmax":2250000,"ymax":1220000,"spatialReference":{"wkid":102008}});
map = new esri.Map("map", {extent:startExtent});
var dynUrl = "http://MyServer:6080/arcgis/rest/services/USA/ConterminousUSA/MapServer";
layer = new esri.layers.ArcGISDynamicMapServiceLayer(dynUrl, { "id": "ConterminousUSA" });
if (layer.loaded) {
buildLayerList(layer);
}
else {
dojo.connect(layer, "onLoad", buildLayerList);
}
var printUrl = "http://MyServer:6080/arcgis/rest/services/USA/AdvancedHighQualityPrinting/GPServer/AdvancedHighQualityPrinting";
printTask = new esri.tasks.PrintTask(printUrl, {async: true});
params = new esri.tasks.PrintParameters();
params.map = map;
}
function buildLayerList(layer) {
var items = dojo.map(layer.layerInfos,function(info,index){
if (info.defaultVisibility) {
visible.push(info.id);
}
return "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" + info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label>";
});
dojo.byId("layer_list").innerHTML = items.join();
layer.setVisibleLayers(visible);
map.addLayer(layer);
}
function updateLayerVisibility() {
var inputs = dojo.query(".list_item"), input;
visible = [];
dojo.forEach(inputs,function(input){
if (input.checked) {
visible.push(input.id);
}
});
//if there aren't any layers visible set the array to be -1
if(visible.length === 0){
visible.push(-1);
}
layer.setVisibleLayers(visible);
}
function print(){
var layout = dojo.byId("layout");
var index = layout.selectedIndex;
var selectedValue_layout = layout.options[index].value;
var format = dojo.byId("format");
var index = format.selectedIndex;
var selectedValue_format = format.options[index].value;
var georef_info = dojo.byId("georef_info");
var index = georef_info.selectedIndex;
var selectedValue_georef_info = georef_info.options[index].value;
var legendLayer = new esri.tasks.LegendLayer();
legendLayer.layerId = "ConterminousUSA";
legendLayer.subLayerIds = [];
if (CapitalCities.checked == true)
{
legendLayer.subLayerIds.push(0);
}
if (InterstateHighways.checked == true)
{
legendLayer.subLayerIds.push(1);
}
if (Rivers.checked == true)
{
legendLayer.subLayerIds.push(2);
}
if (Lakes.checked == true)
{
legendLayer.subLayerIds.push(3);
}
if (StateBoundaries.checked == true)
{
legendLayer.subLayerIds.push(4);
}
params.template = {layout: selectedValue_layout, format: selectedValue_format, layoutOptions: {legendLayers:[legendLayer]}};
params.extraParameters = {Georef_info: selectedValue_georef_info};
printTask.execute(params, printComplete);
}
function printComplete(result){
window.open(result.url);
}
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
<div id="map" style="width:1000px; height:600px; border:1px solid #000;"></div>
<br />
Layout Template:
<select id="layout" >
<OPTION value="CentralUSA">CentralUSA</OPTION>
<OPTION value="ConterminuousUSA">ConterminuousUSA</OPTION>
<OPTION value="NortheasternUSA">NortheasternUSA</OPTION>
<OPTION value="NorthwesternUSA">NorthwesternUSA</OPTION>
<OPTION value="SouthernUSA">SouthernUSA</OPTION>
<OPTION value="SouthwesternUSA">SouthwesternUSA</OPTION>
</select>
Format:
<select id="format">
<OPTION value="PDF">PDF</OPTION>
<OPTION value="PNG">PNG</OPTION>
</select>
Include Georef info?
<select id="georef_info">
<OPTION value="True">True</OPTION>
<OPTION value="False">False</OPTION>
</select>
<br /><br />
Table of Contents: <br /><span id="layer_list"></span>
<br /><br />
Include in Legend: <br />
<label><input type="checkbox" name="CapitalCities" checked="checked">Capital Cities</label>
<label><input type="checkbox" name="InterstateHighways" checked="checked">Interstate Highways</label>
<label><input type="checkbox" name="Rivers" checked="checked">Rivers</label>
<label><input type="checkbox" name="Lakes" checked="checked">Lakes</label>
<label><input type="checkbox" name="StateBoundaries" checked="checked">State Boundaries</label>
<br /><br />
<input type="button" id="print" value="Print" onclick="print();"/>
</body>
</html>
Run the web application
Run the web application that you created in the previous step. Consult the ArcGIS web API documentation for instructions on running web applications, if necessary. A screen capture of the ArcGIS for Silverlight web API application is below. Note that Capital Cities is turned off in the Table of Contents. Also note that State Boundaries is turned off in Include in Legend.
![Completed Silverlight web application Completed Silverlight web application](0155/GUID-9DD8BE54-BB42-4165-BD46-C77CC8F1E1C0-web.png)
After clicking the Export Map button, an output file will automatically pop up after a moment. The illustration below shows a sample printer-friendly output PDF. Note that layers that were turned off in the Table of Contents and Include in Legend in the web application are reflected in the output. Moreover, the PDF uses high-quality vector data that was staged in the layout templates instead of tiled cache images of service layers. This produces higher quality cartographic output. The output file will also contain other elements that were staged in the template map documents: a legend, data frame and coordinate system dynamic text, and a scale bar. If the user selected the Georef info parameter to be True, then the output PDF will also contain georeferencing information which can be viewed in Adobe Reader using the Geospatial Location Tool.
![Output PDF Output PDF](0155/GUID-7504FAA6-C2C2-4372-83D3-06E8E383778C-web.png)
This concludes the advanced high-quality web map printing/exporting tutorial. For a basic web map printing tutorial, see the basic high-quality web map printing/exporting tutorial.