0
votes

I have a dialog, within the dialog there is a tableviewer thats shows the results from user actions. I have created a method that has a print button. The print code include is sample code I have found from examples.

final Text t = new Text(composite, SWT.BORDER | SWT.MULTI);
   Button localPrintersButton = new Button(composite, SWT.PUSH);
   localPrintersButton.setText("Print Results");
   localPrintersButton.addSelectionListener(new SelectionAdapter() {
       public void widgetSelected(SelectionEvent e) {
          PrintDialog printDialog = new PrintDialog(Display.getCurrent().getActiveShell(), 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.computeTrim(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= t.getText( );
              Point extent = gc.stringExtent(printText);
              gc.drawString(printText, leftMargin, topMargin + 
                      font.getFontData( )[0].getHeight( ));
              p.endPage( );
              gc.dispose( );
              p.endJob( );
              p.dispose( );
          }
       }
   }); 

When my dialog opens there is a text box next to the print button. I can type something in the text box, select the print button and select my local printer, then it prints the contents of the text box.

I am trying to figure out how to print out the table instead of the textbox Is this possible?

2

2 Answers

1
votes

I think you are looking for

Control.class
public boolean print (GC gc)
0
votes

Perhaps you can create a GC from the Table widget and use GC#copyArea(Image, int, int) to create an image of the table to print.

If the table has scroll bars and not all data is visible, you might need to do some extra work to create multiple images and stitch them together. Hope this helps.