MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

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

        PrintDialog dlg = new PrintDialog(shell);

        dlg.setScope(PrinterData.SELECTION);

        PrinterData printerData = dlg.open();
        if (printerData != null) {
            Printer printer = new Printer(printerData);
            System.out.println(printer.getClientArea());

            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.

            printer.dispose();
        }

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