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