风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

实现SWT打印表格与图片功能(ZT)

转载自 http://hi.baidu.com/gridrender/blog/item/0fff0f335b52ef44ac4b5f43.html

源代码下载地址:
参考网址:
(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的报表库: SWT Report,支持报表打印功能:
1. 跨行和跨列功能
2. 页码和页数统计
3. 边距和间距调整
4. 各边框颜色设置
5. 前景和背景颜色
6. 自适应页面大小
其中,CustomReportTest 类生成的报表


SWT提供的打印功能很简单,特别是在做表格打印的时候,需要大家使用GC自己绘出来,才能打印,对于初级的开发人员和人力不足的公司来说是非常麻烦的事情。


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;

/**
* 通用表格打印组件,目前提供两个方法分别用于打印表格(Gird,Table);
* 工作任务名:printContent
* @author lign
*
*/
public class PrintContent   {

/**
* 对Gird进行打印操作
    * @param grid SWT 的nebula项目的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);
      
    }
  
   /**
    * 对Table进行打印操作
    * @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);
    
}

/**
    * 处理打印以及调用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();
    }
}

参考网址:
(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.blogjava.net/Javawind/articles/129899.html

和打印文字不同。因为系统中的dpi(dot per inch)和打印机的dpi不同,所以要进行转换。

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 风人园 阅读(1932) 评论(2)  编辑  收藏 所属分类: SWT

评论

# re: 实现SWT打印表格与图片功能(ZT)[未登录]  回复  更多评论   

saf
2010-10-11 16:19 | wewe

# re: 实现SWT打印表格与图片功能(ZT)[未登录]  回复  更多评论   

博主你好,我调用了你的打印表格的方法,出现了如下错误:
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

只有注册用户登录后才能发表评论。


网站导航: