Print Task Sample

In this post, Bryan Baker comments on a new sample you can use to add a Print task to your Web ADF applications: 

A sample print task for the Web ADF for the Microsoft .NET Framework is now available at the ESRI Developer Network website. This sample allows users to do simple printing of a map and task results. ESRI plans to include printing in the upcoming 9.3 version, but this sample should get you started with printing from the Web ADF. The sample works in the Web ADFs for both ArcGIS Server and ArcIMS.

The sample task prints all resources and services in the Map control, including graphics layers and scalebar. It merges the resources into a single image that the browser displays in a new window. If the user has generated task results, such as with a query or identify, the user may also choose to print those results as a table below the map. The sample also allows entering a custom title. It does not currently support printing other items in the website, such as the table of contents or overview map.

In the website, the Map may be zoomed to any location, and other map properties may be set, such as turning layers on/off or displaying highlights from task results. If other tasks, such as a QueryAttributes task, have been added, the print task displays a list of results from those tasks to print. From the Print task dialog, the user may set a print title and task results to print (if any have been generated in the website). Clicking Print in the dialog pops up a new browser window with the title, map and task results (if any). The user may then print the page with their usual printing methods, to any available printer.

Some additional highlights:

  • The user may choose print size, resolution and scale. Resolution (e.g., 300 dpi) is accomplished via standard HTML in the browser.
  • The administrator can configure properties of the task, such as the default print page title, the maximum print size, whether to allow printing task results, and whether to print the scale bar on the map.

Installing the sample Print Task

You can install and use the sample task as-is, or customize it for your own use. To install the task you need to:

  1. Make sure the Web ADF is already installed, with Service Pack 1 or higher applied.
  2. Unzip the downloaded file.
  3. Register the task with the .NET global assembly cache.
  4. (Optional) Add the task to the Visual Studio toolbox.
  5. (Optional) Add the task to Manager.

Details for these steps are in the instructions at the EDN website, and also in the Readme.doc file accompanying the sample. The sample includes the source code for the task, which can be customized. (Note: If you downloaded the EDN sample before the date of this post, you should revisit the EDN site for updated instructions for adding the task to Manager.)

The instructions on EDN include the path to the ArcGIS Server Manager. To add the task to the ArcIMS Web Manager, follow the same instructions, except add the appropriate entry into the Tasks.xml file for the ArcIMS Web Manager, which by default is in C:InetpubwwwrootArcIMSManagerApplicationsApp_Data.

If you install a service pack for the Web ADF, you will need to reinstall the task into Manager. Uninstalling the task must be done manually; see the readme document for details.

Adding the sample Print Task to a Website

After you install the task, you can add it to a Web ADF web page, using either Manager or Visual Studio. In Manager, you should see the Print task in the list of available tasks when you create or edit a website. Add it to the list of tasks in the website, then configure it (you must click Configure for any task, even if you just keep the default settings). The configuration settings allows you to set properties such as the maximum allowed size and resolution and default title for the print page. When you save the website, the print task should appear in the menu of tasks in the website.

The Print task appears in the left menu of the Web mapping application

From Visual Studio, add the Print task like other tasks, usually into a TaskManager. Set the Print task's results container to a TaskResults control on the page, using the "smart tag" in the upper right of the task. You can set other properties in the Properties window of Visual Studio. You can set the task's Visible property to false if desired, and make sure a Menu or TreeView control is buddied to the TaskManager so the user sees a list of tasks to open.

The task currently requires that the web page include a Map control. If no Map control is present, an error will occur. The code could be customized to allow for a page with only tasks, such as a query task, with no map. Also, please note that the task will not work with ArcGIS Online services (currently in beta), since those services currently deliver maps only as cached tiles (this task requests new map images for printing).

This task does not use the layout of any ArcGIS Server service in the website. If you want to print using an MXD-based layout, you might want to check out Ismael Chivite's sample posted at ArcScripts that prints layouts.

We'd welcome your comments on this sample. The download page for the sample includes a user comments section. We'll also post periodic updates to the sample based on feedback.

You can try out the task yourself by printing a map from this example web application.

This entry was posted in Services and tagged , , , , . Bookmark the permalink.

Leave a Reply

12 Comments

  1. sgourley says:

    I think you should add support for adding a legend dynamically for layers that are turned on. Also, The text map title is kind of hokey, editing the title in the page layout looks nicer crisper.

    What would be really nice would be if you could change the size of the paper, and the map would stretch to fit the page size. Almost like setting it to 100% width 100% height instead of a static pixel height and width. When using the page layout arc objects, the page size changes but the objects do not stretch to fit – they stay the same size as they were created in the page layout of the mxd.

  2. bb1769 says:

    We’re looking into adding legend and other items for printing in 9.3. The issue right now is that the controls do not have an export-to-image method, so for a legend, you’d have to construct it by iterating through the resources and layers and outputting the swatches and labels.

    On the auto-sizing question, the sample creates a standard web page, rather than a PDF or other document. Web pages do not have a paper-page size per se – that is determined by how you choose to print the page. So it would not really be feasible to specify a page size in this context. The sample’s map-width size in real units (inches/cm) partially addresses that issue.

    Please keep in mind that this is a sample, not a finished product, though hopefully it will help users as-is without having to tackle code. Developers are welcome to customize and extend it for their own needs.

  3. gigemboy says:

    In response to my earlier post, the place to modify the url to place an ampersand instead of a question mark if your map url already includes a query string is in the GenerateAndStoreMap() procedure in PagePrinter.vb (line 259). It would probably be best to examine the current URL to see whether a query string exists, and then append the correct character based on that.

  4. sterlingdq says:

    Tim- As mentioned near the end of the post, this task will not work with ArcGIS Online services because the task requests a new image from the server. ArcGIS Online services are configured to return only pre-rendered tiles and will not respond to the ExportMapImage request used by the Print Task.

  5. bb1769 says:

    The print task sample should work with SP 4. When service packs are installed, there are also policy files installed that allow components from earlier versions to work with the later version numbers. If you find problems, let us know.

  6. syednaimath says:

    Hi all, This sample is working with SP4, but its throwing callback exception when user types in Arabic title, while opening print page window. Any help to solve this problem will be appreciated.
    Regards

  7. marracci says:

    Hi,

    We’re getting this error:

    Error file generated by LayoutSOE

    Template:D:GISXXXXXXArcServerXXXXXX Print Layout.mxd

    Output File:D:gisoutputarcgisoutput367a8884-eb02-41dc-9e28-d65d987adef4.pdf

    Error on function ‘Export. Error description: Value does not fall within the expected range.

    The error occurs after the layout “generates successfully” in the Results panel. I’m curious as to whether you’ve seen this error before. Thanks

  8. marracci says:

    Seems that we figured out what might be causing the trouble. We updated the map service’s representative .mxd locally instead of on the arcGIS Server. Unfortunately, we updated to SP4 locally, yet the Server is still on SP2.

    When we uploaded the SP4 .mxd to the server and restarted the map service, the print function’s end result failed.

    We got around this by re-editing a vintage SP2 .mxd directly on the server (thank you Archive) and re-starting the service.

    Short story: never mix Service Packs, ESRI hasn’t figured out how to create them in a symbiotic way.

  9. csergent06 says:

    I have the same issue as another developer at: http://forums.esri.com/Thread.asp?c=158&f=1701&t=231407

    Is there an issue with the Print Task, IE7, or HP printers?

    We are currently using SP2 on ESRI software, but I have no problems in Firefox.

    I really would like to know what causes this.

    Thanks,

    Chris

  10. csergent06 says:

    Another HP print problem in the forums:
    http://forums.esri.com/Thread.asp?c=93&f=1731&t=243729

    Chris

  11. arvinder2005 says:

    Hi,

    The print preview window in Firefox does not have a print button. Try opening the example application mentioned above in firefox. Create a print preview page. I get a print button in IE but not in Firefox.

    Thanks,
    Chris