Print active view
arcgissamples\mapbean\commands\PrintActiveViewCommand.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.mapbean.commands;

import java.io.File;
import java.io.IOException;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.swing.JOptionPane;

import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.Page;
import com.esri.arcgis.controls.BaseCommand;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.display.IOutputRasterSettings;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IEnvelope;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.output.EmfPrinter;
import com.esri.arcgis.output.IPrinter;
import com.esri.arcgis.output.Paper;
import com.esri.arcgis.system.tagRECT;

public class PrintActiveViewCommand extends BaseCommand {
  private static final long serialVersionUID = 1L;
  HookHelper hookHelper = null;

  public PrintActiveViewCommand() {
    String devKitHome = null;
    try {
      //Get DEVKITHOME Home
      devKitHome = System.getenv("AGSDEVKITJAVA");
    } catch (Exception e) {
      System.out.println("AGSDEVKITJAVA environment variable is not set.");
    }
    
    bitmapPath = devKitHome + File.separator + "java" + File.separator + "samples" +
                   File.separator + "data" + File.separator + "visualbeans" +
                   File.separator + "mapbean" + File.separator + "bunny.bmp";
  }

  public String getCaption() {
    return "Print Active View";
  }

  public String getTooltip() {
    return "Sends the current Focus Map to the default printer";
  }

  public boolean isEnabled() {
    return true;
  }

  public void onClick() {

    try {
      IActiveView doc = hookHelper.getActiveView();
      IPrinter docPrinter = new EmfPrinter();
      int prevOutputImageQuality = 3;
      IOutputRasterSettings docOutputRasterSettings = (IOutputRasterSettings) doc
          .getScreenDisplay().getDisplayTransformation();
      tagRECT deviceRECT = new tagRECT();
      Paper docPaper = new Paper();
      IEnvelope docPrinterBounds = new Envelope();
      Envelope visibleBounds = new Envelope();

      docPrinter.setPaperByRef(docPaper);
      docOutputRasterSettings.setResampleRatio(prevOutputImageQuality);

      PrintService ps = PrintServiceLookup.lookupDefaultPrintService();

      if (ps == null) {
        // if we get an unexpected result, return.
        JOptionPane.showMessageDialog(null, new String(
            "Error getting default printer info, exiting..."));
        return;
      }

      docPaper.setPrinterName(PrintServiceLookup
          .lookupDefaultPrintService().getName());

      // make sure the paper orientation is set to the orientation
      // matching the current view.
      docPaper.setOrientation(hookHelper.getPageLayout().getPage()
          .getOrientation());

      // set the spoolfilename (this is the job name that shows up in the
      // print queue)
      docPrinter.setSpoolFileName("PrintActiveViewSample");

      Page page = (Page) hookHelper.getPageLayout().getPage();
      page.getDeviceBounds(docPrinter, Short.parseShort("1"), 0.0,
          docPrinter.getResolution(), docPrinterBounds);

      deviceRECT.bottom = (int) docPrinterBounds.getYMax();
      deviceRECT.left = (int) docPrinterBounds.getXMin();
      deviceRECT.right = (int) docPrinterBounds.getXMax();
      deviceRECT.top = (int) docPrinterBounds.getYMin();

      docPrinterBounds.putCoords(0, 0,
          deviceRECT.right - deviceRECT.left, deviceRECT.bottom
              - deviceRECT.top);

      // get paper object
      docPaper = (Paper) docPrinter.getPaper();
      if (docPaper == null) {
        JOptionPane.showMessageDialog(null, "no paper defined");
        return;
      }

      // create an envelope for the bounds of the printer's page size in
      // device units.
      IEnvelope deviceFrame = new Envelope();
      page.getDeviceBounds(docPrinter, Short.parseShort("1"), 0.0,
          docPrinter.getResolution(), deviceFrame);

      // create and populate an envelope with the bounds of the printer
      // page.
      IEnvelope wksdevice = new Envelope();
      deviceFrame.queryEnvelope(wksdevice);

      // transfer the envelope to the deviceRECT.
      deviceRECT.left = (int) Math.round(wksdevice.getXMin());
      deviceRECT.top = (int) Math.round(wksdevice.getYMin());
      deviceRECT.right = (int) Math.round(wksdevice.getXMax());
      deviceRECT.bottom = (int) Math.round(wksdevice.getYMax());

      visibleBounds = null;

      // Here's where the printing actually begins:
      // ==========================================
      // The docPrinter.startPrinting() method returns the hDC of the
      // printer, which we then
      // use as the hDC parameter of activeview.output. ActiveView.Output
      // is the function that
      // draws the content on the active view, at the specified
      // resolution, to the deviceRECT specified.
      // VisibleBounds can be null, which means "print the active view",
      // or can be set to a zoom extent.

      doc.output(docPrinter.startPrinting(docPrinterBounds, 0),
              docPrinter.getResolution(), deviceRECT,
              visibleBounds, null);

      // here we call FinishPrinting, which actually flushes the output to
      // the printer.
      docPrinter.finishPrinting();

    } catch (AutomationException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public void onCreate(Object arg0) {
    super.onCreate(arg0);

    try {
      hookHelper = new HookHelper();
      hookHelper.setHookByRef(arg0);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}