Java打印pdf不';行不通
我正在尝试使用本地打印机打印PDF文件。问题是,当我在“打印机”对话框中选择打印机并按下“打印”按钮时,什么也没发生,对话框就关闭了。我没有收到任何异常或警告,打印机只是没有打印PDF文件。我不知道为什么会这样。我已经用三台不同的电脑(Windows7和10)和三台不同的打印机测试过了,但总是出现同样的问题 我还使用PDFBox库进行了如下尝试:Java打印pdf不';行不通,java,swing,pdf,printing,java-print,Java,Swing,Pdf,Printing,Java Print,我正在尝试使用本地打印机打印PDF文件。问题是,当我在“打印机”对话框中选择打印机并按下“打印”按钮时,什么也没发生,对话框就关闭了。我没有收到任何异常或警告,打印机只是没有打印PDF文件。我不知道为什么会这样。我已经用三台不同的电脑(Windows7和10)和三台不同的打印机测试过了,但总是出现同样的问题 我还使用PDFBox库进行了如下尝试: try{ FileInputStream fis = new FileInputStream("C:/Users/Self/Desktop/t
try{
FileInputStream fis = new FileInputStream("C:/Users/Self/Desktop/test.pdf");
PDDocument doc = new PDDocument();
doc.load(fis);
doc.print();
}catch(FileNotFoundException ex1){
ex1.printStackTrace();
}catch(IOException ex2){
ex2.printStackTrace();
}catch(PrinterException ex3){
ex3.printStackTrace();
}
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, aset);
if(ps.length == 0){
throw new IllegalStateException("No Printer found");
}
PrintService myService = null;
for (PrintService printService : ps) {
if (printService.getName().equals("HP17DC1C (HP Officejet Pro 8610)")) {
myService = printService;
break;
}
}
if (myService == null) {
throw new IllegalStateException("Printer not found");
}
try{
FileInputStream fis = new FileInputStream("C:/Users/Slef/Desktop/test.pdf");
Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
DocPrintJob printJob = myService.createPrintJob();
printJob.print(pdfDoc, aset);
fis.close();
}catch(FileNotFoundException ex1){
ex1.printStackTrace();
}catch(PrintException ex2){
ex2.printStackTrace();
}catch(IOException ex3){
ex3.printStackTrace();
}
使用以下标准java方式:
try{
FileInputStream fis = new FileInputStream("C:/Users/Self/Desktop/test.pdf");
PDDocument doc = new PDDocument();
doc.load(fis);
doc.print();
}catch(FileNotFoundException ex1){
ex1.printStackTrace();
}catch(IOException ex2){
ex2.printStackTrace();
}catch(PrinterException ex3){
ex3.printStackTrace();
}
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, aset);
if(ps.length == 0){
throw new IllegalStateException("No Printer found");
}
PrintService myService = null;
for (PrintService printService : ps) {
if (printService.getName().equals("HP17DC1C (HP Officejet Pro 8610)")) {
myService = printService;
break;
}
}
if (myService == null) {
throw new IllegalStateException("Printer not found");
}
try{
FileInputStream fis = new FileInputStream("C:/Users/Slef/Desktop/test.pdf");
Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
DocPrintJob printJob = myService.createPrintJob();
printJob.print(pdfDoc, aset);
fis.close();
}catch(FileNotFoundException ex1){
ex1.printStackTrace();
}catch(PrintException ex2){
ex2.printStackTrace();
}catch(IOException ex3){
ex3.printStackTrace();
}
但这两种方法都会导致相同的问题。在打印对话框之后,什么也没有发生。我使用的是java版本8.66。有人能帮我解决这个问题吗,我真的需要打印PDF文件吗?可能是这些LIB检查了打印权限,PDF激活了打印限制吗?你能不能请检查一下类似的问题是否对你有帮助,@不,打印限制没有激活。我可以使用adobe reader手动打印PDF文件,但不能使用java。@AlekhyaVemavarapu我在stackoverflow上尝试了很多方法,但所有解决方案都会导致相同的问题:/