Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:54:33+00:00 2026-06-14T06:54:33+00:00

I have a Print Button in my SWT TitleAreaDialog. viewPDFButton.addSelectionListener(new SelectionAdapter() { public void

  • 0

enter image description here

I have a Print Button in my SWT TitleAreaDialog.

viewPDFButton.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
        try {

           startPdfPrintOperation();
        }
        catch (Exception e1) {
           // TODO Auto-generated catch block
           e1.printStackTrace();
        }
     }
  }); 

I am getting a existing PDF filename and path from a user selection from a table.
I am then wantinf to print the pdf file out to a local printer. The user needs to be allowed to select the local printer of choice.

public void startPdfPrintOperation() throws Exception {
  File file = new File(getPDFFileName());
  RandomAccessFile raf;
  raf = new RandomAccessFile(file, "r");
  FileChannel channel = raf.getChannel();
  ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
  pdfFile = new PDFFile(buf);
  PDFPrintPage pages = new PDFPrintPage(pdfFile);

  // Create Print Job
  pjob = PrinterJob.getPrinterJob();
  pjob.setPrintable(new MyPrintable());
  final HashPrintRequestAttributeSet attset;
  attset = new HashPrintRequestAttributeSet ();
  attset.add (new PageRanges (1, pdfFile.getNumPages ()));
  if (pjob.printDialog (attset)) {
      try {
           pjob.print (attset);
      }
      catch (PrinterException e) {
           e.printStackTrace();
      }
  }             
}
class MyPrintable implements Printable {
  public int print (Graphics g, PageFormat format, int index) throws PrinterException {
     int pagenum = index+1;
        if (pagenum < 1 || pagenum > pdfFile.getNumPages ())
            return NO_SUCH_PAGE;

        Graphics2D g2d = (Graphics2D) g;
        AffineTransform at = g2d.getTransform ();
        PDFPage pdfPage = pdfFile.getPage (pagenum);

        Dimension dim;
        dim = pdfPage.getUnstretchedSize ((int) format.getImageableWidth (),
                                       (int) format.getImageableHeight (),
                                       pdfPage.getBBox ());

        Rectangle bounds = new Rectangle ((int) format.getImageableX (),
                                       (int) format.getImageableY (),
                                       dim.width,
                                       dim.height);

        PDFRenderer rend = new PDFRenderer (pdfPage, (Graphics2D) g, bounds, null, null);

        try
        {
           pdfPage.waitForFinish ();
           rend.run ();
        }
        catch (InterruptedException ie)
        {
           MessageDialog.openError(null, "PDF Error Message", "Needs");
        }
        g2d.setTransform (at);
        g2d.draw (new Rectangle2D.Double (format.getImageableX (),
                                       format.getImageableY (),
                                       format.getImageableWidth (),
                                       format.getImageableHeight ()));
     return PAGE_EXISTS;
  }
}

I am getting the above error from line 315

if (pjob.printDialog (attset)) {

The printer dialog opens the entire application is frozen and goes unresponsive. Then in about 30 secs, I get the above error.

I have tried to use Display.getDefault().asyncExec(new Runnable() ) in multiple spots but that did not help.

Could it be because the base dialog is SWT and the printer dialog is AWT?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:54:35+00:00Added an answer on June 14, 2026 at 6:54 am

    Since you didn’t define “in multiple spots”, I would suggest you refactor the print job in its own class, extends Thread and implement starting the print job in the run method.
    I’m not familiar with all classes in above code, you can try simply starting this thread and it will run in parallel with the SWT thread. Try avoiding shared ressources, this may help resolving your deadlock. If you want to have a UI response from this thread, you can wrap e.g. the SWT messagebox (“Printing done!”) in a call to Display.getDefault().asyncExec(new Runnable() { ... }.
    Furthermore, please test if the printing code without UI code produces the same exception. If it does, the environment may be misconfigured.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript backed 'print' button on my webpage where the user can
I am trying to have an onclick button print a check and run my
I want to have a Print This Image button on the page where the
let say i have a print button on couple of pages and whenever user
I want to have a button Print all reports which will print 5 different
I have a print button on my page that will run window.print(), it just
I have a webpage with a print button here: http://vitaminjdesign.com/IAM/plant-care-info/sod-care/ When you press the
In the jQuery 'ready' block, I have: // bind print work order button to
I have added a print button to my page. <button type=button class=printBtn>Print This Page</button>
A print button on a site I am working on opens a new window

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.