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 8920515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:17:25+00:00 2026-06-15T06:17:25+00:00

I created a class to print a PDF file. The class opens a application

  • 0

I created a class to print a PDF file.

The class opens a application window with a start button.

When the start button is clicked it opens a ProgressMonitorDialog that runs my PrintFile Class that allows the users to select a local printer and sends the printjob to my print method.

Everything runs correctly but the printjob is never sent to the printer.

I know that is gets past the print call

try {
    System.out.println("before\n");         
    pjob.print();
    System.out.println("after\n"); 
}

But the print method is never executed? So I am not sure what pjob.print(); is doing.

 public class AplotPdfPrintLocal extends ApplicationWindow {

   private String userSelectedFile;

   /////////////////////////////////////////////////////////////////////////
   //                         Constructor                                 //
   /////////////////////////////////////////////////////////////////////////
   public AplotPdfPrintLocal(String pdfFilePath) {
      super(null);
      this.userSelectedFile = "c:/Teamcenter/Tc8.3/portal/temp/james.pdf";
   }

   /////////////////////////////////////////////////////////////////////////
   //                             run()                                   //
   /////////////////////////////////////////////////////////////////////////
   public void run() {
      setBlockOnOpen(true); // Don't return from open() until window closes
      open();  // Opens the main window
      Display.getCurrent().dispose();  // Dispose the display
   }

/////////////////////////////////////////////////////////////////////////
//                         configureShell()                            //
/////////////////////////////////////////////////////////////////////////
protected void configureShell(Shell shell) {
   super.configureShell(shell);
   shell.setText("Print PDF");
}

/////////////////////////////////////////////////////////////////////////
//                         createContents()                            //
/////////////////////////////////////////////////////////////////////////
protected Control createContents(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new GridLayout(1, true));

  //final Button indeterminate = new Button(composite, SWT.CHECK);
  //indeterminate.setText("Indeterminate");

  // Create the ShowProgress button
  Button showProgress = new Button(composite, SWT.NONE);
  showProgress.setText("Select Printer");

  final Shell shell = parent.getShell();

  // Display the ProgressMonitorDialog
  showProgress.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
        try {
           new ProgressMonitorDialog(shell).run(true, true, new PrintFile(userSelectedFile, true));
        } 
        catch (InvocationTargetException e) {
           MessageDialog.openError(shell, "Error", e.getMessage());
        } 
        catch (InterruptedException e) {
           MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
        }
     }
   });
   parent.pack();
   return composite;
}

//===============================================================
//    PrintFile Class
// =============================================================== 
class PrintFile extends Thread implements Printable, IRunnableWithProgress  {

  private String filename;
  private Boolean setupPaper;
  private File file; 
  private PrinterJob pjob;
  private FileInputStream fis;
  private FileChannel fc;
  private ByteBuffer bb;
  private PDFFile pdfFile;
  private PDFPrintPage pages;
  private PageFormat pfDefault;
  private Label pagenumlabel;

  public PrintFile(String filename, boolean setupPaper) {
     this.filename = filename;
     this.setupPaper = setupPaper;
  }

  /////////////////////////////////////////////////////////////////////////
  //                              run()                                  //
  /////////////////////////////////////////////////////////////////////////
  @Override
  public void run(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
     try {
        System.out.println("File: " + filename + "\n");
        file = new File(filename);
        fis = new FileInputStream(file);
        fc = fis.getChannel();
        bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        pdfFile = new PDFFile(bb); 
        pages = new PDFPrintPage(pdfFile);

        pjob = PrinterJob.getPrinterJob();
        pfDefault = PrinterJob.getPrinterJob().defaultPage();
        Paper defaultPaper = new Paper();
        defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(), defaultPaper.getHeight());
        pfDefault.setPaper(defaultPaper);
        if (setupPaper) {
           pfDefault = PrinterJob.getPrinterJob().pageDialog(pfDefault);
        }
        pjob.setJobName(file.getName());
        //System.out.println("File Name : " + file.getName() + "\n");
        if (pjob.printDialog()) {
           pfDefault = pjob.validatePage(pfDefault);
           Book book = new Book();
           book.append(pages, pfDefault, pdfFile.getNumPages());
           pjob.setPageable(book);
           try {
              pjob.print();
           }
           catch (PrinterException e) {
              e.printStackTrace();
           }
        }
     }
     catch (FileNotFoundException e) {
        e.printStackTrace();
     }
     catch (IOException e) {
        e.printStackTrace();
     }
  }

  /////////////////////////////////////////////////////////////////////////
  //                             print()                                 //
  /////////////////////////////////////////////////////////////////////////
  @Override
  public int print(Graphics g, PageFormat format, int index) throws PrinterException {
     System.out.println("Got in the Print\n");       
     int pagenum = index + 1;
     if ((pagenum >= 1) && (pagenum <= pdfFile.getNumPages())) {
        if (pagenumlabel != null) {
           pagenumlabel.setText(String.valueOf(pagenum));
        }
        Graphics2D g2 = (Graphics2D) g;
        PDFPage page = pdfFile.getPage(pagenum);
        double pwidth = format.getImageableWidth();
        double pheight = format.getImageableHeight();
        double aspect = page.getAspectRatio();
        double paperaspect = pwidth / pheight;
        if (paperaspect < 1.0) {
           switch (format.getOrientation()) {
              case PageFormat.REVERSE_LANDSCAPE:
              case PageFormat.LANDSCAPE:
                   format.setOrientation(PageFormat.PORTRAIT);
                   break;
              case PageFormat.PORTRAIT:
                   format.setOrientation(PageFormat.LANDSCAPE);
                   break;
           }
           pwidth = format.getImageableWidth();
           pheight = format.getImageableHeight();
           paperaspect = pwidth / pheight;
        }
        Rectangle imgbounds;
        int width;
        int height;
        if (aspect > paperaspect) {
           height = (int) (pwidth / aspect);
           width = (int) pwidth;
        } 
        else {
           width = (int) (pheight * aspect);
           height = (int) pheight;
        }
        imgbounds = new Rectangle((int) format.getImageableX(),
               (int) format.getImageableY(),
               width, height);

        PDFRenderer pgs = new PDFRenderer(page, g2, imgbounds, null, null);
        try {
           page.waitForFinish();
           pgs.run();
        } 
        catch (InterruptedException ie) {
        }
          return PAGE_EXISTS;
     } 
     else {
        return NO_SUCH_PAGE;
     }
   }
 }
}

I feel like I am missing something simple, but have no clue what it is?
I am really stuck on this issue and really need help to figure this out.

Thanks to all that can help me

EDIT
If I comment out the following code

if (pjob.printDialog()) {

The print comes right out!

But when it is not commented out, the printer selection dialog opens, click the OK button,
printer dialog closes and nothing ever happens

EDIT

After some testing, I have discovered the problem has something with the pjob.printDialog().

I removed the if statment and set it up like this.

 boolean ok = pjob.printDialog();
    System.out.println("OK value " + ok + "\n");
    //if (ok) {
       pfDefault = pjob.validatePage(pfDefault);
       Book book = new Book();
       book.append(pages, pfDefault, pdfFile.getNumPages());
       System.out.println("Print Job After : " + pjob + "\n");
       pjob.setPageable(book);
       try {
          pjob.print();
       }
       catch (PrinterException e) {
          e.printStackTrace();
       }

printDialog opens, I click OK – A print job is sent to the printer, but nothing prints out.
I click cancel – The print job is printed out as wanted

  • 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-15T06:17:27+00:00Added an answer on June 15, 2026 at 6:17 am

    I am not 100% for sure why, but if I use the cross platform printDialog instead of a native printDialog – The code works as expected.

    This is the working code

               PrinterJob pjob = PrinterJob.getPrinterJob();
               PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
               PageFormat pfDefault = pjob.pageDialog(aset);
               pjob.setJobName(file.getName());
               pages.show(pjob);
               if (pjob.printDialog(aset)) {
                  pfDefault = pjob.validatePage(pfDefault);
                  Book book = new Book();
                  book.append(pages, pfDefault, pdfFile.getNumPages());
                  pjob.setPageable(book);
                  try {
    
                     pjob.print(aset);
    
                  }
                  catch (PrinterException exc) {
                     System.out.println(exc);
                  }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to print hello world. For that i created class and return
I've created class that takes Exception type in constructor private readonly Exception _exception; public
I created a class that extends DefaultMutableTreeNode. It has a variable, resource of the
I created a class that's something like this: public class MovieTheaterList { public DateTime
I created a class CMyClass whose CTor takes a UCHAR as argument. That argument
I am trying to make a button in html that when it is clicked,
I'm having trouble many PDF files in the order that I send the print
I want to save the switch state. I created custom cell class witch print
I'm trying to create a method that will send a PDF file directly to
I created a class binary search tree. but the problem is when i print

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.