我已经看了几篇文章,并整理了一些代码来打印.pdf文件。然而,有些地方出了问题。程序结束时没有任何错误,但不会打印任何内容:
public static void print(String file) {
FileInputStream psStream = null;
try {
psStream = new FileInputStream(file);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
System.out.println("service found: "+svcName);
if (svcName.equals("Win32 Printer : Microsoft XPS Document Writer")){
myPrinter = services[i];
System.out.println("Destination printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
}我正在尝试将文档发送到Microsoft XPS文档编写器进行测试,但我不认为这会导致打印文件失败。
发布于 2015-04-30 11:05:40
如果这能行得通,我会很震惊。Java打印驱动程序实现通常只在打印机上流式传输文档。如果打印机恰好具有本机PDF处理能力,那么它将打印PDF,但这是所有打印机中相当有限的子集(我无法想象XPS文档编写器打印驱动程序具有这种能力-但谁知道呢,也许我在这一点上是错的)。
FWIW,我们的产品之一是一个大批量打印管理系统,我们最终不得不使用本机代码来可靠地打印PDF文件。
https://stackoverflow.com/questions/29958224
复制相似问题