我试图使用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()上。不会引发异常,也不会出现打印对话框。它什么也做不了。以前有人遇到过这个问题吗?
规格:,PDFBox v1.8.9,JDK1.8.0_05,HP Photosmart打印机
发布于 2015-08-11 20:40:37
对于任何有同样问题的人。当我将所有的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();
}https://stackoverflow.com/questions/31530145
复制相似问题