我创建了一个类来打印 PDF 文件。
该类打开一个带有开始按钮的应用程序窗口。
单击开始按钮时,它会打开一个运行我的 PrintFile 类的 ProgressMonitorDialog,该类允许用户选择本地打印机并将打印作业发送到我的打印方法。
一切运行正常,但打印作业永远不会发送到打印机。
我知道那是通过打印电话
try {
System.out.println("before\n");
pjob.print();
System.out.println("after\n");
}
但是 print 方法永远不会执行?所以我不确定什么 pjob.print(); 是在做。
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;
}
}
}
}
我觉得我错过了一些简单的东西,但不知道它是什么?
我真的被这个问题困住了,真的需要帮助来解决这个问题。
感谢所有可以帮助我的人
编辑 如果我注释掉以下代码
if (pjob.printDialog()) {
打印出来了!
但是当它没有被注释掉时,打印机选择对话框打开,单击 OK 按钮,打印机对话框关闭并且什么也没有发生
编辑
经过一些测试,我发现问题出在 pjob.printDialog() 上。
我删除了 if 语句并像这样设置它。
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 打开,我单击 OK - 打印作业被发送到打印机,但没有打印出来。我单击取消 - 根据需要打印打印作业