Printing Graphics : Print « SWT « Java Tutorial






scale any images according to the printer's DPI to fit the page.

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MainClass {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);

    Image image = new Image(display, "fileName.gif");
    ImageData imageData = image.getImageData();

    PrintDialog dialog = new PrintDialog(shell, SWT.NULL);
    PrinterData printerData = dialog.open();

    Printer printer = new Printer(printerData);

    Point screenDPI = display.getDPI();
    Point printerDPI = printer.getDPI();
    int scaleFactor = printerDPI.x / screenDPI.x;

    Rectangle trim = printer.computeTrim(0, 0, 0, 0);

    if (printer.startJob("fileName")) {
      if (printer.startPage()) {
        GC gc = new GC(printer);
        Image printerImage = new Image(printer, imageData);

        // Draw the image
        gc.drawImage(printerImage, 0, 0, imageData.width, imageData.height, -trim.x, -trim.y,
            scaleFactor * imageData.width, scaleFactor * imageData.height);

        // Clean up
        printerImage.dispose();
        gc.dispose();
        printer.endPage();
      }
    }
    // End the job and dispose the printer
    printer.endJob();
    printer.dispose();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}








17.110.Print
17.110.1.Printing
17.110.2.Get the entire printable area of the page for the selected printer
17.110.3.Place one-inch margins on the page
17.110.4.Printing Text
17.110.5.Printing Graphics
17.110.6.Print DPI
17.110.7.Print out String
17.110.8.Print text to printer, with word wrap and pagination