PDFBox 在 Print() 命令处冻结
PDFBox Freezes at Print() Command
我正在尝试使用 PDFBox 打印现有的 PDF 文件。这是代码:
public void sendToPrinter(){
File PDFFile = new File("Example.pdf");
try {
PDDocument pd = PDDocument.load(PDFFile);
pd.print();
pd.close();
} catch (IOException | PrinterException ex) {
System.out.println("Error: Couldn't find pdf or printers");
}
}
然而,当我 运行 它时,程序冻结在 pd.print()
。没有异常被抛出,没有打印对话框出现。它只是什么都不做。以前有人遇到过这个问题吗?
规格:Mac OS X Yosemite、PDFBox v1.8.9、JDK1.8。0_05、HP Photosmart 打印机
对于遇到同样问题的任何人。当我将所有 PDF 工作放到另一个线程时,我的 print()
命令起作用了。供参考:
public void sendToPrinter() {
//Create new Task
Task task = new Task<Boolean>() {
@Override
public Boolean call() {
//Reference the PDF file
File PDFFile = new File("File.pdf");
try {
//Load PDF & create a Printer Job
PDDocument pd = PDDocument.load(PDFFile);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(pd));
//Show native print dialog & wait for user to hit "print"
if (job.printDialog()) {
job.print();
}
pd.close();
} catch (IOException | PrinterException ex) {
}
return true;
}
};
//Run task on new thread
new Thread(task).start();
}
我正在尝试使用 PDFBox 打印现有的 PDF 文件。这是代码:
public void sendToPrinter(){
File PDFFile = new File("Example.pdf");
try {
PDDocument pd = PDDocument.load(PDFFile);
pd.print();
pd.close();
} catch (IOException | PrinterException ex) {
System.out.println("Error: Couldn't find pdf or printers");
}
}
然而,当我 运行 它时,程序冻结在 pd.print()
。没有异常被抛出,没有打印对话框出现。它只是什么都不做。以前有人遇到过这个问题吗?
规格:Mac OS X Yosemite、PDFBox v1.8.9、JDK1.8。0_05、HP Photosmart 打印机
对于遇到同样问题的任何人。当我将所有 PDF 工作放到另一个线程时,我的 print()
命令起作用了。供参考:
public void sendToPrinter() {
//Create new Task
Task task = new Task<Boolean>() {
@Override
public Boolean call() {
//Reference the PDF file
File PDFFile = new File("File.pdf");
try {
//Load PDF & create a Printer Job
PDDocument pd = PDDocument.load(PDFFile);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(pd));
//Show native print dialog & wait for user to hit "print"
if (job.printDialog()) {
job.print();
}
pd.close();
} catch (IOException | PrinterException ex) {
}
return true;
}
};
//Run task on new thread
new Thread(task).start();
}