Demonstrates printing images : Print « SWT JFace Eclipse « Java

Demonstrates printing images

//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (
//Robert Harris (

import org.eclipse.swt.*;
import org.eclipse.swt.printing.*;
import org.eclipse.swt.widgets.*;

 * This class demonstrates printing images
public class ImagePrinterExample {
   * The application entry point
   * @param args the command line arguments
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display, SWT.NONE);

    try {
      // Prompt the user for an image file
      FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
      String fileName =;

      if (fileName == null) { return; }

      // Load the image
      ImageLoader loader = new ImageLoader();
      ImageData[] imageData = loader.load(fileName);

      if (imageData.length > 0) {
        // Show the Choose Printer dialog
        PrintDialog dialog = new PrintDialog(shell, SWT.NULL);
        PrinterData printerData =;

        if (printerData != null) {
          // Create the printer object
          Printer printer = new Printer(printerData);
          // Calculate the scale factor between the screen resolution and printer
          // resolution in order to correctly size the image for the printer
          Point screenDPI = display.getDPI();
          Point printerDPI = printer.getDPI();
          int scaleFactor = printerDPI.x / screenDPI.x;
          // Determine the bounds of the entire area of the printer
          Rectangle trim = printer.computeTrim(0, 0, 0, 0);

          // Start the print job
          if (printer.startJob(fileName)) {
            if (printer.startPage()) {
              GC gc = new GC(printer);
              Image printerImage = new Image(printer, imageData[0]);
              // Draw the image
              gc.drawImage(printerImage, 0, 0, imageData[0].width,
                imageData[0].height, -trim.x, -trim.y, 
                scaleFactor * imageData[0].width, 
                scaleFactor * imageData[0].height);
              // Clean up
          // End the job and dispose the printer
    } catch (Exception e) {
      MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR);
      messageBox.setMessage("Error printing test image");;

Related examples in the same category

1.Print KTable (SWT Table)Example Print KTable (SWT Table)Example
2.Simple PrintSimple Print
3.Printing ExamplePrinting Example
5.Demonstrates printing text
6.SWT Print and 2D GraphicsSWT Print and 2D Graphics
7.Print text to printer, with word wrap and paginationPrint text to printer, with word wrap and pagination
8.Print Hello World in black, outlined in red, to default printerPrint Hello World in black, outlined in red, to default printer