如何在 Java 应用程序中打印 PDF 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2478890/
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
How to print PDF file in a Java application?
提问by user293914
How do I print a PDF file from a Java application?
如何从 Java 应用程序打印 PDF 文件?
回答by sfrj
Here some source code that will allow you print any text file:
这里有一些源代码可以让你打印任何文本文件:
public void print() {
//The desktop api can help calling other applications in our machine
//and also many other features...
Desktop desktop = Desktop.getDesktop();
try {
//desktop.print(new File("DocXfile.docx"));
desktop.print(new File("Docfile.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
Maybe it suit your needs since you did not give more details.
也许它适合您的需求,因为您没有提供更多详细信息。
回答by Gareth Davis
Try PDF Renderer. It's open source and there are a couple of examples on the site on how to render to a printer device.
试试PDF 渲染器。它是开源的,网站上有几个关于如何渲染到打印机设备的示例。
回答by Andrei Ciobanu
I've used PDFBoxbefore for a similar task like yours. It's an excellent library from the Apache Software Foundation. The class you are probably going to use is called: PDFTextStripper. The javadoc for the class can be found here.
我以前使用PDFBox执行过与您类似的任务。它是来自Apache 软件基金会的优秀库。您可能要使用的类称为:PDFTextStripper。可以在此处找到该类的 javadoc 。

