java 如何自动打印 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2191183/
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 PDFs automatically
提问by pledge
We have a number of systems that produce PDFs that need to be printed. These are stored on a central document store. A message then goes onto a JMS queue that the document needs printing. A service, written in Java , picks these up and then invokes a native command. This is to call Adobe Reader with the /t flag. This causes the document to print without the GUI showing.
我们有许多系统可以生成需要打印的 PDF。这些存储在中央文档存储中。然后,一条消息进入文档需要打印的 JMS 队列。用 Java 编写的服务获取这些信息,然后调用本机命令。这是使用 /t 标志调用 Adobe Reader。这会导致文档在不显示 GUI 的情况下打印。
However since a power cut this no longer works. In the interim we are having to manually print hundreds of documents. We originally tried using Java printing, but the PDFs came out malformed.
然而,由于停电,这不再有效。在此期间,我们不得不手动打印数百份文档。我们最初尝试使用 Java 打印,但输出的 PDF 格式不正确。
What is a better solution to this?
对此有什么更好的解决方案?
采纳答案by mark stephens
This code only works if the printer supports PDF. Otherwise you need to use a native printer or a Java library. There is a blog article on this at http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java
此代码仅在打印机支持 PDF 时有效。否则,您需要使用本机打印机或 Java 库。在http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java上有一篇关于此的博客文章
回答by Alex
Ever since Java 1.5, Sun developed a pdf renderer library for handling PDF. Now this one is left to Swing Labs. And not sure whether this one would be added into future java APIs. http://java.net/projects/pdf-renderer/
从 Java 1.5 开始,Sun 就开发了一个用于处理 PDF 的 pdf 渲染器库。现在这个留给 Swing Labs。并且不确定是否会将此添加到未来的 Java API 中。 http://java.net/projects/pdf-renderer/
It is used to view or print pdf files. to print pdf files, you can call this libray. Here is some part of the code.
它用于查看或打印 pdf 文件。要打印 pdf 文件,您可以调用此库。这是代码的一部分。
File input = new File(docName);
FileInputStream fis = new FileInputStream(input);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile curFile=null;
PDFPrintPage pages=null;
curFile = new PDFFile(bb); // Create PDF Print Page
pages = new PDFPrintPage(curFile);
PrinterJob pjob = PrinterJob.getPrinterJob();
PrintService[] services = pjob.lookupPrintServices();
for(PrintService ps:services){
String pName = ps.getName();
if(pName.equalsIgnoreCase("PrinterName")){
pjob.setPrintService(ps);
System.out.println(pName);
break;
}
}
pjob.setJobName(docName);
Book book = new Book();
PageFormat pformat = PrinterJob.getPrinterJob().defaultPage();
book.append(pages, pformat, curFile.getNumPages());
pjob.setPageable(book);
// print
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// Print it
pjob.print(aset);
回答by user11153
You can use Apache PDFBox. Examples:
您可以使用Apache PDFBox。例子:
a) Printing PDF as Pageable
a) 将 PDF 打印为 Pageable
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPageable pdfPageable = new PDFPageable(pdDocument);
SimpleDoc doc = new SimpleDoc(pdfPageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
printJob.print(doc, null);
b) Printing PDF as Printable
b) 将 PDF 打印为 Printable
This option has advantage that you can control page dimensions, margins, etc. by modifying pageFormatvariable.
此选项的优点是您可以通过修改pageFormat变量来控制页面尺寸、边距等。
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPrintable pdfPrintable = new PDFPrintable(pdDocument);
Book book = new Book();
book.append(pdfPrintable, pageFormat);
SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
printJob.print(doc, null);
回答by Adeel Ansari
Show us the code. I remember printing PDF with no issues using Java Print API. Below is the code, might need some modification, but should run as it is,
向我们展示代码。我记得使用 Java Print API 打印 PDF 没有问题。下面是代码,可能需要一些修改,但应该按原样运行,
InputStream in = new FileInputStream(file);
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// find the printing service
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("FX", null));
attributeSet.add(new Copies(1));
PrintService[] services = PrintServiceLookup.lookupPrintServices(
DocFlavor.INPUT_STREAM.PDF, attributeSet);
//create document
Doc doc = new SimpleDoc(in, flavor, null);
// create the print job
PrintService service = services[0];
DocPrintJob job = service.createPrintJob();
// monitor print job events
PrintJobWatcher watcher = new PrintJobWatcher(job);
System.out.println("Printing...");
job.print(doc, null);
// wait for the job to be done
watcher.waitForDone();
System.out.println("Job Completed!!");
Note:
笔记:
Flavoris not needed in 2 places, 1 place should be enough. You find that out.PrintJobWatcheris a nested class, to add aPrintJobListener.
Flavor2个地方不需要,1个地方就够了。你发现了。PrintJobWatcher是一个嵌套类,添加一个PrintJobListener.
回答by Lalith
Try using ICEpdf. Here's an example from documentation page:
Document pdf = new Document();
pdf.setFile(filePath);
// create a new print helper with a specified paper size and print
// quality
PrintHelper printHelper = new PrintHelper(null, pdf.getPageTree(),
0f, MediaSizeName.NA_LEGAL, PrintQuality.DRAFT);
// try and print pages 1 - 10, 1 copy, scale to fit paper.
printHelper.setupPrintService(selectedService, 0, 0, 1, true);
// print the document
printHelper.print();

