swt打印專題先貼出陳剛編寫的<eclipse 從入門到精通>中的一個(gè)打印的例子,,然后對(duì)細(xì)節(jié)做講解:
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; 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.Display; import org.eclipse.swt.widgets.Shell; public class PrintTextExample { public static void main(String[] args) { try { PrintTextExample window = new PrintTextExample(); window.open(); } catch (Exception e) { e.printStackTrace(); } } public void open() { final Display display = Display.getDefault(); final Shell shell = new Shell(); String fileName = "C:\\Documents and Settings\\Nie Hanzi\\桌面\\新建 文本文檔.txt"; if (fileName != null) { //用打印對(duì)話框取得一個(gè)打印對(duì)話printer PrintDialog printDlg = new PrintDialog(shell); PrinterData printerData = printDlg.open(); if (printerData != null) { Printer printer = new Printer(printerData); //打印文件 try { String contents = getFileContents(fileName);//取出文件內(nèi)容 MyPrinter p = new MyPrinter(printer, fileName, contents); p.print(); } catch (java.lang.Exception e) { e.printStackTrace(); } printer.dispose();//printer對(duì)象沒(méi)用后要手動(dòng)dispose掉 } } display.dispose(); } /** * 取出文件內(nèi)容的方法 */ private String getFileContents(String fileName) throws FileNotFoundException, IOException { StringBuffer contents = new StringBuffer(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(fileName)); while (reader.ready()) { contents.append(reader.readLine()); contents.append("\n"); //每讀完一行,,換行一次 } } finally { if (reader != null) try { reader.close(); } catch (IOException e) { } } return contents.toString(); } /** * 自定義的打印實(shí)現(xiàn)類,,將所需參數(shù)傳入,,調(diào)用print方法即可打印 */ private class MyPrinter { private Printer printer; // 打印對(duì)象 private String fileName; //文件名(帶路徑) private String contents; //文件名對(duì)應(yīng)的文件內(nèi)容 private GC gc; //一個(gè)GC對(duì)象 private int xPos, yPos; //打印對(duì)象printer用的坐標(biāo) private Rectangle bounds; //打印空間 private StringBuffer buf; // Holds a word at a time private int lineHeight; // The height of a line of text /** * 構(gòu)造函數(shù) */ public MyPrinter(Printer printer, String fileName, String contents) { this.printer = printer; this.fileName = fileName; this.contents = contents; } /** * 打印文件的方法 */ public void print() { if (printer.startJob(fileName)) {//開(kāi)始打印任務(wù) //設(shè)定打印空間 bounds = computePrintArea(printer); xPos = bounds.x; yPos = bounds.y; //創(chuàng)建GC對(duì)象 gc = new GC(printer); //設(shè)定線的高度 lineHeight = gc.getFontMetrics().getHeight(); //設(shè)定tab鍵的空格數(shù) int tabWidth = gc.stringExtent(" ").x; //開(kāi)始打印 printer.startPage(); buf = new StringBuffer(); char c; for (int i = 0, n = contents.length(); i < n; i++) { //得到文件內(nèi)容的字符 c = contents.charAt(i); //如果讀到\n,調(diào)用printBuffer方法將buf中的字符打印,,并換行 if (c == ‘\n‘) { printBuffer(); printNewline(); } //如果讀到\t,,表示要跳過(guò)一定的空格 else if (c == ‘\t‘) { xPos += tabWidth; } else { buf.append(c);//將字符添加進(jìn)buf變量 //檢查是否有空間,如果則打印 if (Character.isWhitespace(c)) printBuffer(); } } printer.endPage(); printer.endJob(); gc.dispose(); } } /** * 打印緩存buf變量中的字符 */ private void printBuffer() { //取得緩存中的字符寬度 int width = gc.stringExtent(buf.toString()).x; //如果寬度不夠,,則換行 if (xPos + width > bounds.x + bounds.width) printNewline(); //打印緩存buf變量中的字符 gc.drawString(buf.toString(), xPos, yPos, false); xPos += width; buf.setLength(0); } /** * 打印換行的方法 */ private void printNewline() { //重設(shè)新行的坐標(biāo) xPos = bounds.x; yPos += lineHeight; //如果超過(guò)頁(yè)長(zhǎng)度,,則換一頁(yè)打印 if (yPos > bounds.y + bounds.height) { yPos = bounds.y; printer.endPage(); printer.startPage(); } } /** * 計(jì)算打印空間的方法 */ private Rectangle computePrintArea(Printer printer) { //取得打印空間 Rectangle rect = printer.getClientArea(); Rectangle trim = printer.computeTrim(0, 0, 0, 0); //取得打印機(jī)的DPI(DPI:表示每英寸的點(diǎn)數(shù),即通常說(shuō)的打印機(jī)的分辨率) Point dpi = printer.getDPI(); // 計(jì)算可打印空間 int left = trim.x + dpi.x; if (left < rect.x) left = rect.x; int right = (rect.width + trim.x + trim.width) - dpi.x; if (right > rect.width) right = rect.width; int top = trim.y + dpi.y; if (top < rect.y) top = rect.y; int bottom = (rect.height + trim.y + trim.height) - dpi.y; if (bottom > rect.height) bottom = rect.height; return new Rectangle(left, top, right - left, bottom - top); } } } printer.computeTrim(0, 0, 0, 0)返回不可用的區(qū)域 DPI:表示每英寸的點(diǎn)數(shù),即通常說(shuō)的打印機(jī)的分辨率 例如A4值的高是297mm,寬是210mm,,加入DPI是(300,,300),每英寸為25.4mm,,折算成象素就是(3508,2480),。 |
|