Java by API/org.eclipse.swt.printing/Printer

Материал из Java эксперт
Перейти к: навигация, поиск

new Printer(PrinterData printerData)

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.ruputeTrim(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.jexp.ru";
        Point extent = gc.stringExtent(printText);
        gc.drawString(printText, leftMargin, topMargin + 
                font.getFontData()[0].getHeight());
        p.endPage();
        gc.dispose();
        p.endJob();
        p.dispose();
    }
  }
}





Printer: computeTrim(int x, int y, int width, int height)

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.ruputeTrim(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.jexp.ru";
        Point extent = gc.stringExtent(printText);
        gc.drawString(printText, leftMargin, topMargin + 
                font.getFontData()[0].getHeight());
        p.endPage();
        gc.dispose();
        p.endJob();
        p.dispose();
    }
  }
}





Printer: getDPI()

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.ruputeTrim(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.jexp.ru";
        Point extent = gc.stringExtent(printText);
        gc.drawString(printText, leftMargin, topMargin + 
                font.getFontData()[0].getHeight());
        p.endPage();
        gc.dispose();
        p.endJob();
        p.dispose();
    }
  }
}





Printer: startJob(String jobName)

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.ruputeTrim(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.jexp.ru";
        Point extent = gc.stringExtent(printText);
        gc.drawString(printText, leftMargin, topMargin + 
                font.getFontData()[0].getHeight());
        p.endPage();
        gc.dispose();
        p.endJob();
        p.dispose();
    }
  }
}





Printer: startPage()

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.ruputeTrim(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.jexp.ru";
        Point extent = gc.stringExtent(printText);
        gc.drawString(printText, leftMargin, topMargin + 
                font.getFontData()[0].getHeight());
        p.endPage();
        gc.dispose();
        p.endJob();
        p.dispose();
    }
  }
}