Support for printing in the Runtime SDK for Java

To provide support for map printing in the ArcGIS Runtime SDK for Java, the map control class JMap implements Java's Pageable interface. This allows you to make use of the printing functionality built in to the Java standard library via the java.awt.print package (Java 2D printing API) and the javax.print package (Java Print Service API).

In particular, you can set your map, a JMap object, as a PrinterJob's pageable document:

PrinterJob printJob = PrinterJob.getPrinterJob();

The contents of your JMap ('map', above), including all the layers and any overlays appearing on the map, can thus be printed by calling the print() method on the PrinterJob instance, as will be shown in what follows.

Print settings

In order to print the contents of the map, you need to have set a page format on the map. The print job will need to know information about how many pages to print based on the paper size, the margins, and so on. One way to accomplish this is to start off by setting what paper and page format to use, then pass these settings through to the map. Below is some example code, and for more information on the Paper and PageFormat classes, please refer to the Java 2D Printing API documentation at java.awt.print.

Paper paper = new Paper();
// ...set up the paper options...
PageFormat pageFormat = new PageFormat();
// ...set up page format options and pass the paper settings through

// set page format for the map to print
// now you can get number of pages if desired
int numPages = map.getNumberOfPages();

You've now created and set up your Paper and PageFormat instances with the desired settings, and set the page format for the map, as shown above. You are ready to create a print job and send the job to the desired printer:

PrinterJob printJob = PrinterJob.getPrinterJob();
try {
   // set a print service
   // send job to printer!
} catch (PrinterException e) {
   // handle the exception

In the code sample above, though the process of selecting a printer is not shown, a call to the static method PrinterJob.lookupPrintServices() will return any available printers to use and one can be chosen from this list.

More on printing

Using the Java 2D Printing API and the Java Print Service API, there are many ways to work through the details of printing the contents of the JMap. One example was shown above where settings are passed in programatically before printing. Another way would be to use convenience methods in the aforementioned Java SE printing APIs which pop up a printing dialog. You could also make use of your own custom overlays to add for example a title to your map, as overlays are printed along with the JMap. In the ArcGIS Runtime for Java Sample Application, you can play with an interactive sample for printing a map, view the source code, and modify it for your own purposes.