Print out String : Print « SWT « Java Tutorial






import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
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 StringPrint {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.open();
    PrinterData data = Printer.getDefaultPrinterData();
    if (data == null) {
      System.out.println("Warning: No default printer.");
    }
    Printer printer = new Printer(data);
    if (printer.startJob("SWT Printing Snippet")) {
      Color black = printer.getSystemColor(SWT.COLOR_BLACK);
      GC gc = new GC(printer);
      if (printer.startPage()) {
        gc.setForeground(black);
        String testString = "Hello World!";
        gc.drawString(testString, 200, 200);
        printer.endPage();
      }
      gc.dispose();
      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