[SWT] Print & Print Preview

转自: http://www.blogjava.net/Javawind/articles/132177.html

从SWT中建立"print dialog", 当call print()时弹出print dialog

public   void  print() {
        PrintDialog dialog 
= new PrintDialog(da.sShell, SWT.NONE);
        PrinterData data 
= dialog.open();
        
if (data == nullreturn;
        
if (data.printToFile) {
            data.fileName 
= "print.out"// you probably want to ask the user for a filename
        }

        printer 
= new Printer(data);
        print(printer);
        printer.dispose();
    }

紧接着建立print(Printer printer)
private   void  print(Printer printer)  {
        
if (printer.startJob("Text")) {   // the string is the job name - shows up in the printer's job list
            Rectangle trim = printer.computeTrim(0000);
            
//Calculate the scale factor between the screen resolution and printer
            
// resolution in order to correctly size the image for the printer
            Point screenDPI = da.sShell.getDisplay().getDPI();
            Point printerDPI 
= printer.getDPI();
            
int scaleFactor = printerDPI.x / screenDPI.x;
            
//int width = image.getImageData().width;
            
//int height = image.getImageData().height;
            /* Create printer GC, and create and set the printer font */
            gc 
= new GC(printer); 
            gc.drawText("some text here", scaleFactor 
* 20, scaleFactor * 20);
            
            printer.endJob();
            gc.dispose();
        }

    }



完成"print" 之后添加"print preview"。只需要让"print" method 带入 2 个arguments:Device 和 GC
当打印时带入Printer device 和 包含打印信息的GC。当要预览时带入 Display device 和GC(
drawing on canvas),就是相当于把要打印的东西显示在canvans里。

create all colors, fonts, and images using the correct device,之后运行gc.dispose()

对于打印的字体的设置
gc.setFont( new  Font())


对于打印的字体的设置可以参考:
org.eclipse.swt.custom.StyledText
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.custom.StyleRange;
import  org.eclipse.swt.custom.StyledText;
import  org.eclipse.swt.graphics.Font;
import  org.eclipse.swt.layout.GridData;
import  org.eclipse.swt.layout.GridLayout;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Shell;

public   class  SampleStyledText  {
  Display display 
= new Display();
  Shell shell 
= new Shell(display);
  
  StyledText styledText;

  
public SampleStyledText() {
    init();
    
    shell.setLayout(
new GridLayout());
    
    styledText 
= new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    
    styledText.setLayoutData(
new GridData(GridData.FILL_BOTH));
    
    Font font 
= new Font(shell.getDisplay(), "Courier New"12, SWT.NORMAL);
    styledText.setFont(font);
    
    styledText.setText(
"123456789/r/nABCDEFGHI");
    
    StyleRange styleRange1 
= new StyleRange();
    styleRange1.start 
= 2;
    styleRange1.length 
= 16;
    styleRange1.foreground 
= shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    styleRange1.background 
= shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange1.fontStyle 
= SWT.BOLD;
    
    StyleRange styleRange2 
= new StyleRange();
    styleRange2.start 
= 14;
    styleRange2.length 
= 3;
    styleRange2.fontStyle 
= SWT.NORMAL;
    styleRange2.foreground 
= shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange2.background 
= shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    
//    styledText.setStyleRange(styleRange1);
//    styledText.setStyleRange(styleRange2);
    
    
//styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
    
//styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});
    
    
//styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    
//    styledText.setSelection(4);
//    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
//    styledText.insert("000");
    
    
    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    
//    styledText.setStyleRanges(new StyleRange[]{styleRange1});
//    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    
    
//shell.pack();
    shell.setSize(300120);
    shell.open();
    
//textUser.forceFocus();

    
// Set up the event loop.
    while (!shell.isDisposed()) {
      
if (!display.readAndDispatch()) {
        
// If no more entries in event queue
        display.sleep();
      }

    }


    display.dispose();
  }

  
  
private String printStyleRanges(StyleRange[] styleRanges) {
    
    
if(styleRanges == null)
      
return "null";
    
else if(styleRanges.length == 0)
      
return "[]";
    
    StringBuffer sb 
= new StringBuffer();
    
for(int i=0; i<styleRanges.length; i++{
      sb.append(styleRanges[i] 
+ "/n");
    }

    
    
return sb.toString();
  }

  
public static void main(String[] args) {
    
new SampleStyledText();
  }

}





参考:

You will have to think about margins, page breaks, and scaling, and you
may want to think about whether or not the chosen fonts exist on the printer.
For an optimization, you might want to use a set of background images, one
per page, for the display drawing so that your user can flip between the 'pages'
without you having to render them again.

Printing is a lot of work. The widgets don't do it for you - you have to do
all the drawing yourself. Once you have done the work, however, it is a
relatively simple thing to add a "print preview" feature.

The closest thing in SWT to an example of this is the StyledText widget -
have a look at the code in org.eclipse.swt.custom.StyledText. It doesn't do
a print preview specifically, but it does know how to draw itself to the
screen, and to print itself to a printer. Print preview would just be a bit
of a twist on drawing to the screen.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值