<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    風(fēng)人園

    弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
    隨筆 - 99, 文章 - 181, 評(píng)論 - 56, 引用 - 0
    數(shù)據(jù)加載中……

    實(shí)現(xiàn)SWT打印表格與圖片功能(ZT)

    轉(zhuǎn)載自 http://hi.baidu.com/gridrender/blog/item/0fff0f335b52ef44ac4b5f43.html

    源代碼下載地址:
    參考網(wǎng)址:
    (1)http://club.xasoft.org/?uid-167-action-viewspace-itemid-346#xspace-itemreply
    (2)http://www.eclipseworld.org/bbs/read-cec-tid-5299-keyword-.html

    純SWT的報(bào)表庫: SWT Report,支持報(bào)表打印功能:
    1. 跨行和跨列功能
    2. 頁碼和頁數(shù)統(tǒng)計(jì)
    3. 邊距和間距調(diào)整
    4. 各邊框顏色設(shè)置
    5. 前景和背景顏色
    6. 自適應(yīng)頁面大小
    其中,CustomReportTest 類生成的報(bào)表


    SWT提供的打印功能很簡(jiǎn)單,特別是在做表格打印的時(shí)候,需要大家使用GC自己繪出來,才能打印,對(duì)于初級(jí)的開發(fā)人員和人力不足的公司來說是非常麻煩的事情。


    import org.ceclipse.reporting.IReport;
    import org.ceclipse.reporting.IReportPage;
    import org.ceclipse.reporting.Report;
    import org.ceclipse.reporting.ReportData;
    import org.ceclipse.reporting.ReportUtil;
    import org.eclipse.nebula.widgets.grid.Grid;
    import org.eclipse.swt.printing.PrintDialog;
    import org.eclipse.swt.printing.Printer;
    import org.eclipse.swt.widgets.Table;
    import org.eclipse.ui.PlatformUI;

    /**
    * 通用表格打印組件,目前提供兩個(gè)方法分別用于打印表格(Gird,Table);
    * 工作任務(wù)名:printContent
    * @author lign
    *
    */
    public class PrintContent   {

    /**
    * 對(duì)Gird進(jìn)行打印操作
        * @param grid SWT 的nebula項(xiàng)目的Grid
    * @param title 表頭文字描述
        */
       public static void printGird(Grid grid, String title) {
            IReportPage page = ReportUtil.convert(grid, title);
            Report report = new Report();
           report.addPage(page);
          printToPrinter(report);
          
        }
      
       /**
        * 對(duì)Table進(jìn)行打印操作
        * @param table SWT 的Table
        * @param title 表頭文字描述
        */
       public static void printTable(Table table, String title) {
           IReportPage page = ReportUtil.convert(table, title);
           Report report = new Report();
          report.addPage(page);
           printToPrinter(report);
        
    }

    /**
        * 處理打印以及調(diào)用Printer
        * @param report
        */
        private static void printToPrinter(IReport report)   {
              ReportData reportData = report.getReportData();
            reportData.setJobName("printContent");
            reportData.setPrinter(new Printer(new PrintDialog(PlatformUI.getWorkbench

    ().getActiveWorkbenchWindow().getShell()).open()));
             report.print();
        }
    }

    參考網(wǎng)址:
    (1)http://club.xasoft.org/?uid-167-action-viewspace-itemid-346#xspace-itemreply
    (2)http://www.eclipseworld.org/bbs/read-cec-tid-5299-keyword-.html
    (3)http://www.tkk7.com/Javawind/articles/129899.html

    和打印文字不同。因?yàn)橄到y(tǒng)中的dpi(dot per inch)和打印機(jī)的dpi不同,所以要進(jìn)行轉(zhuǎn)換。

    import org.eclipse.swt.*;
    import org.eclipse.swt.graphics.*;
    import org.eclipse.swt.printing.*;
    import org.eclipse.swt.widgets.*;

    /** *//**
    * This class demonstrates printing images
    */
    public class ImagePrinterExample {
    /** *//**
       * The application entry point
       * @param args the command line arguments
       */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display, SWT.NONE);

        try {
          // Prompt the user for an image file
          FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
          String fileName = fileChooser.open();

          if (fileName == null) { return; }

          // Load the image
          ImageLoader loader = new ImageLoader();
          ImageData[] imageData = loader.load(fileName);

          if (imageData.length > 0) {
            // Show the Choose Printer dialog
            PrintDialog dialog = new PrintDialog(shell, SWT.NULL);
            PrinterData printerData = dialog.open();

            if (printerData != null) {
              // Create the printer object
              Printer printer = new Printer(printerData);

              // Calculate the scale factor between the screen resolution and printer
              // resolution in order to correctly size the image for the printer
              Point screenDPI = display.getDPI();
              Point printerDPI = printer.getDPI();
              int scaleFactor = printerDPI.x / screenDPI.x;

              // Determine the bounds of the entire area of the printer
              Rectangle trim = printer.computeTrim(0, 0, 0, 0);

              // Start the print job
              if (printer.startJob(fileName)) {
                if (printer.startPage()) {
                  GC gc = new GC(printer);
                  Image printerImage = new Image(printer, imageData[0]);
                 
                  // Draw the image
                  gc.drawImage(printerImage, 0, 0, imageData[0].width,
                    imageData[0].height, -trim.x, -trim.y,
                    scaleFactor * imageData[0].width,
                    scaleFactor * imageData[0].height);

                  // Clean up
                  printerImage.dispose();
                  gc.dispose();
                  printer.endPage();
                }
              }
              // End the job and dispose the printer
              printer.endJob();
              printer.dispose();
            }
          }
        } catch (Exception e) {
          MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR);
          messageBox.setMessage("Error printing test image");
          messageBox.open();
        }
    }
    }

    posted on 2009-04-15 15:04 風(fēng)人園 閱讀(1947) 評(píng)論(2)  編輯  收藏 所屬分類: SWT

    評(píng)論

    # re: 實(shí)現(xiàn)SWT打印表格與圖片功能(ZT)[未登錄]  回復(fù)  更多評(píng)論   

    saf
    2010-10-11 16:19 | wewe

    # re: 實(shí)現(xiàn)SWT打印表格與圖片功能(ZT)[未登錄]  回復(fù)  更多評(píng)論   

    博主你好,我調(diào)用了你的打印表格的方法,出現(xiàn)了如下錯(cuò)誤:
    Exception in thread "main" java.lang.IllegalStateException: Workbench has not been created yet.
    at org.eclipse.ui.PlatformUI.getWorkbench(PlatformUI.java:92)
    at com.ytkj.kq.print.PrintContent.printToPrinter(PrintContent.java:55)
    at com.ytkj.kq.print.PrintContent.printTable(PrintContent.java:44)
    at com.ytkj.kq.PersonnelData.PersonnelChange$6.widgetSelected(PersonnelChange.java:404)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
    at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at com.ytkj.kq.PersonnelData.PersonnelChange.open(PersonnelChange.java:139)
    at com.ytkj.kq.Main$14.widgetSelected(Main.java:852)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
    at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at com.ytkj.kq.Main.<init>(Main.java:278)
    at com.ytkj.kq.Main.main(Main.java:243)
    能幫忙解決一下嗎,希望能告之解決辦法,我的郵箱:lqmh18@163.com
    在此先謝過了
    2010-10-11 16:25 | zmh

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久精品国产精品亚洲蜜月| 国产成人亚洲综合色影视| 爱爱帝国亚洲一区二区三区| 亚洲高清国产拍精品青青草原| 成人久久免费网站| 亚洲精品二三区伊人久久| 又粗又大又硬又爽的免费视频| 国产精品99精品久久免费| 亚洲最大的成人网站| 中文字幕乱理片免费完整的| 亚洲字幕在线观看| 亚洲国产精品国产自在在线 | 日本人护士免费xxxx视频| 亚洲国产精品婷婷久久| A在线观看免费网站大全| 国产福利免费视频 | 在线成人爽a毛片免费软件| 激情无码亚洲一区二区三区| 国产成A人亚洲精V品无码| 女人18一级毛片免费观看| a级片在线免费看| 亚洲AV无码一区二区三区电影| 国产亚洲av片在线观看播放| 免费看h片的网站| 97亚洲熟妇自偷自拍另类图片| 精品国产免费观看久久久| 亚欧免费一级毛片| 瑟瑟网站免费网站入口| 亚洲人成电影青青在线播放| 国产偷国产偷亚洲清高动态图| 妞干网手机免费视频| 24小时日本韩国高清免费| 一级免费黄色大片| 国产精品亚洲一区二区三区在线观看| 天天摸夜夜摸成人免费视频| 在线观看肉片AV网站免费| 免费观看四虎精品成人| 亚洲第一网站男人都懂| 四虎成人免费大片在线| 国产精品免费精品自在线观看| 在线免费观看伊人三级电影|