从 Java 中静默打印 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1407459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Silent Printing of PDF From Within Java
提问by Paul Reiners
We are looking into silent printing of PDF documents from within Java. The printing will be invoked from the desktop and not through a browser so we cannot use JavaScript. PDF Renderer is an operational solution but their rendering quality is not acceptable. iText does not seem to be pluggable with the Java print service. There are some commercial Java libraries, jPDFPrint by Qoppa, JPedal, and ICEpdf which we have not tried out yet.
我们正在研究从 Java 中静默打印 PDF 文档。打印将从桌面调用,而不是通过浏览器调用,因此我们不能使用 JavaScript。PDF Renderer 是一种可操作的解决方案,但其渲染质量不可接受。iText 似乎不能与 Java 打印服务插入。有一些商业 Java 库,Qoppa 的 jPDFPrint、JPedal 和 ICEpdf,我们还没有尝试过。
Does anybody have any experience with PDF silent printing from Java?
有没有人有使用 Java 进行 PDF 静默打印的经验?
采纳答案by Vineet Reynolds
Apache PDFBox. It is currently in incubation, but the PDF printing functionality has been around before that. Internally, it uses the Java Print Services to create a print job, and it also supports silent printing.
阿帕奇PDFBox。它目前处于孵化阶段,但 PDF 打印功能在此之前就已经存在。在内部,它使用 Java 打印服务来创建打印作业,并且还支持静默打印。
Do note that it requires Fontbox as well, and the current (upcoming 0.8.0 release) has included graceful fallback for documents with Type 0 fonts. Type 1 fonts are printed correctly; however in 0.7.3, attempts to print documents with Type 0 fonts will result in an exception being thrown.
请注意,它也需要 Fontbox,并且当前(即将发布的 0.8.0 版本)已经为 Type 0 字体的文档提供了优雅的回退。Type 1 字体打印正确;但是在 0.7.3 中,尝试使用 Type 0 字体打印文档将导致抛出异常。
回答by RED SOFT ADAIR
Have a look at www.pdflib.com. Its comercial but PDFlib Lite is available for free for open source projects. It has bindings for java.
看看 www.pdflib.com。它的商业版 PDFlib Lite 可免费用于开源项目。它具有 java 的绑定。
回答by Dan Dyer
Maybe I'm misunderstanding, but why not just use the Print Service API directly? The following works for me (assumes you have the PDF document as a byte array):
也许我误解了,但为什么不直接使用打印服务 API?以下对我有用(假设您将 PDF 文档作为字节数组):
DocFlavor flavor = DocFlavor.BYTE_ARRAY.PDF;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
if (services.length > 0)
{
DocPrintJob printJob = services[0].createPrintJob();
Doc document = new SimpleDoc(pdfBytes, flavor, null)
printJob.print(document, null);
}
else
{
System.out.println("No PDF printer available.");
}
回答by Dan Dyer
There is an example using JPedal at http://www.jpedal.org/support_egSP.php
在http://www.jpedal.org/support_egSP.php有一个使用 JPedal 的例子
You will need the commercial version of IcePdf if you want full font support.
如果您想要完整的字体支持,您将需要 IcePdf 的商业版本。
回答by Kevin Day
I have experience with making Acrobat (Reader or Full) do the printing, but it's anything but silent (it is unattended, though - just depends on how 'silent' the silent requirement is). If there's interest, I can shoot you the native code that makes the required DDE calls.
我有使用 Acrobat(Reader 或 Full)进行打印的经验,但它绝不是无声的(尽管它是无人看管的 - 仅取决于无声要求的“无声”程度)。如果有兴趣,我可以向您发送进行所需 DDE 调用的本机代码。
回答by rfeague
iText is intended for creating PDF files (per a post I saw from the author), and thus probably isn't what you want.
iText 用于创建 PDF 文件(根据我从作者那里看到的帖子),因此可能不是您想要的。
I've used Qoppa's jPDFPrint quite successfully for exactly this purpose, but it's not cheap. If you can afford it, it's the most robust solution I've found thus far. I've also been very impressed with the level of support; they even generated some custom sample code for me.
为了这个目的,我已经非常成功地使用了 Qoppa 的 jPDFPrint,但它并不便宜。如果您负担得起,这是我迄今为止找到的最强大的解决方案。支持水平也给我留下了深刻的印象。他们甚至为我生成了一些自定义示例代码。
I tried PDFBox, but found that it doesn't support the "Shrink to printable area" page scaling that you get with Acrobat. Not everyone will care about this feature, but it's essential for me.
我尝试了 PDFBox,但发现它不支持使用 Acrobat 获得的“缩小到可打印区域”页面缩放。不是每个人都会关心这个功能,但它对我来说是必不可少的。
回答by user818409
This works for me:
这对我有用:
public void print() {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
FileInputStream psStream = null;
try {
psStream = new FileInputStream("c:\test.pdf");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
if (services.length > 0)
{
PrintService myService = null;
for(PrintService service : services) {
System.out.println(service.getName());
if(service.getName().contains("my printer")) {
myService = service;
break;
}
}
DocPrintJob printJob = myService.createPrintJob();
Doc document = new SimpleDoc(psStream, flavor, null);
try {
printJob.print(document, null);
} catch (PrintException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println("No PDF printer available.");
}
}