new PrintDialog(Shell parent, int style)
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
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.Shell;
public class MainClass {
public static void main(String[] a) {
Shell s = new Shell();
PrintDialog printDialog = new PrintDialog(s, SWT.NONE);
printDialog.setText("Print");
PrinterData printerData = printDialog.open();
if(!(printerData==null))
{
Printer p = new Printer(printerData);
p.startJob("PrintJob");
p.startPage();
Rectangle trim = p.computeTrim(0, 0, 0, 0);
Point dpi = p.getDPI();
int leftMargin = dpi.x + trim.x;
int topMargin = dpi.y / 2 + trim.y;
GC gc = new GC(p);
Font font = gc.getFont();
String printText= "www.java2s.com";
Point extent = gc.stringExtent(printText);
gc.drawString(printText, leftMargin, topMargin +
font.getFontData()[0].getHeight());
p.endPage();
gc.dispose();
p.endJob();
p.dispose();
}
}
}
Related examples in the same category