Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Using AWT Printing from SWT Application
icon9.gif  Using AWT Printing from SWT Application [message #986322] Mon, 19 November 2012 23:54
James Teater is currently offline James TeaterFriend
Messages: 17
Registered: July 2011
Junior Member
I am basically trying to print a existing pdf file from my SWT application using PDFRenderer. PDFRenderer uses AWT and Swing. So here is what I have thus far.

In my SWT Gui Dialog - I have a SWT button (Print PDF).

 final Button printPDFButton = new Button(composite, SWT.PUSH);
      printPDFButton.setText("Print PDF");
      printPDFButton.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
            try {
            catch (Exception e) {
               // TODO Auto-generated catch block

startPDFPrint is a method that calls my Print Class.

 public void startPDFPrint() throws Exception {
      PDFPrintPage pdfPrint = new PDFPrintPage(getShell(), userSelectedFile);;

Then my print class - This is where I am trying to use the SWT - SWING bridge.

public class PDFPrintPage extends ApplicationWindow {
   String fileURL;
   public PDFPrintPage(Shell parentShell, String inputFileName) {
      this.fileURL = inputFileName;

   public void run() {
   protected void configureShell(Shell shell) {
      shell.setText("Printing PDF");
   protected Control createContents(Composite parent) {
      Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
      java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
      javax.swing.JPanel panel = new javax.swing.JPanel( );
      JButton swingButton = new JButton("What the Hell");
      swingButton.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent actionevent) {
           try {
         catch (IOException e) {
            // TODO Auto-generated catch block
      return swtAwtComponent;
   public void printFile(String filename) throws IOException {
      File file = new File(filename);
      FileInputStream fis = new FileInputStream(file);
      FileChannel fc = fis.getChannel();
      ByteBuffer bb =, 0, fc.size());
      PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page

      PrintPage pages = new PrintPage(pdfFile);

      PrinterJob pjob = PrinterJob.getPrinterJob();
      PageFormat pfDefault = PrinterJob.getPrinterJob().defaultPage();
      Paper defaultPaper = new Paper();
      defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(), defaultPaper.getHeight());
      if (pjob.printDialog()) {
          // validate the page against the chosen printer to correct
          // paper settings and margins
          pfDefault = pjob.validatePage(pfDefault);
          Book book = new Book();
          book.append(pages, pfDefault, pdfFile.getNumPages());

          try {
          } catch (PrinterException exc) {
 class PrintPage implements Printable {

      private PDFFile file;

      PrintPage(PDFFile file) {
         this.file = file;

      public int print(Graphics g, PageFormat format, int index) throws PrinterException {
         int pagenum = index + 1;
         if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
            Graphics2D g2 = (Graphics2D) g;
            PDFPage page = file.getPage(pagenum);

            // fit the PDFPage into the printing area
            Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
                  (int) format.getImageableWidth(), (int) format.getImageableHeight());
            g2.translate(0, 0);
            PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
            try {
            } catch (InterruptedException ie) {
               // nothing to do
            return PAGE_EXISTS;
         } else {
            return NO_SUCH_PAGE;


I thought the createContents method was the SWT-SWING bridge because I am calling the printFile method from within the AWT Frame?

Here what happens - The printer selection dialog opens, you can select a local printer and select OK. Watching the printer queue the print job appears and then completes very quickly, but the printer never starts up or prints.

The strange thing is if I comment out
 if (pjob.printDialog())
the job goes to the printer and prints out the PDF.

So it has to be something to do with the printDialog.
Is this a thread issue, does the opening of printDialog run it in a different thread, so there is no printerjob data?

I was reading somewhere that it may need to be called with
 Display.getDefault().asyncExec(new Runnable() {
               public void run() {

Is the SWT AWT bridge done wrong or in the wrong place?
Do I need to incorporate the asyncExec somewhere, Print Class or Gui SWT Class?

I am not getting any errors, there may a time out error, but I have not let it set that long running.

I really need some help solving this issue as soon as possible. My entire project is on hold until I can resolve this.

As stated from above without printDialog() it prints out as it should, but with printDialog() it seems to be sending a empty print job to the printer.

I am also not sure how to close the window with the print is completed?

[Updated on: Mon, 19 November 2012 23:58]

Report message to a moderator

Previous Topic:Simple PDF Viewer using PDFRenderer
Next Topic:SWT/Jface Data binding for search field
Goto Forum:

Current Time: Fri Jun 22 11:40:43 GMT 2018

Powered by FUDForum. Page generated in 0.01837 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top