Place one-inch margins on the page : Print « SWT « Java Tutorial

If you need centimeters instead of inches, multiply each inch value by 2.54 to get the number of centimeters.

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);

    PrintDialog dlg = new PrintDialog(shell);


    PrinterData printerData =;
    if (printerData != null) {
      Printer printer = new Printer(printerData);

      Point dpi = printer.getDPI(); // Get the DPI
      Rectangle rect = printer.getClientArea(); // Get the printable area
      Rectangle trim = printer.computeTrim(0, 0, 0, 0); // Get the whole page
      int left = trim.x + dpi.x; // Set left margin one inch from left side of paper
      int top = trim.y + dpi.y; // Set top margin
      int right = (rect.width + trim.x + trim.width) - dpi.x; // 1st three
                                                              // values give
      // you the right side of the page
      int bottom = (rect.height + trim.y + trim.height) - dpi.y; // Set bottom
                                                                  // margin

      // use left, top, right, and bottom as the boundaries that govern where
      // you draw on the page.


    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {

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