简体   繁体   English

如何在Java中打印PDF文件而不进行打印预览

[英]How to Print PDF file without print preview in java

I generated a PDF file using JasperReports successfully; 我成功使用JasperReports生成了PDF文件; the creation of my PDF file is through a button trigger. 通过按钮触发器创建PDF文件。 After generating the file, I would like to print that PDF file in the printer without the PDF preview (Opening the PDF File then clicking the print button). 生成文件后,我想在没有PDF预览的情况下在打印机中打印该PDF文件(打开PDF文件,然后单击“打印”按钮)。 My goal is when the user clicks the button that generates the PDF, it also will print that PDF in the default printer of that computer. 我的目标是当用户单击生成PDF的按钮时,它还将在该计算机的默认打印机中打印该PDF。 BTW, I'm using Windows for this project and Java JRE7. 顺便说一句,我正在为该项目和Java JRE7使用Windows。

Check this blog http://blog.idrsolutions.com/2010/01/printing-pdf-files-from-java/ ; 检查此博客http://blog.idrsolutions.com/2010/01/printing-pdf-files-from-java/ ; might helpful for you. 可能对您有帮助。

Also you could look for PDFRenderer Java API, might help. 您也可以寻找PDFRenderer Java API,可能会有所帮助。

Edit : How to set print attribute - Ref : http://docs.oracle.com/javase/7/docs/technotes/guides/jps/spec/attributes.fm4.html 编辑:如何设置打印属性-参考: http : //docs.oracle.com/javase/7/docs/technotes/guides/jps/spec/attributes.fm4.html

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(5)); 
aset.add(MediaSize.ISO_A4); 
aset.add(Sides.DUPLEX); 

Pass this attribute to printJob.print(pdfDoc, new HashPrintRequestAttributeSet()) Also, you could try getting the supported attribute first and then use accordingly. 将此属性传递给printJob.print(pdfDoc, new HashPrintRequestAttributeSet())此外,您可以尝试先获取受支持的属性,然后相应地使用。 Please read the above documentation, it is very rich in examples. 请阅读上面的文档,其中包含很多示例。

我使用JPedal成功完成了任务,现在可以使用代码将pdf文件直接以实际大小打印到打印机上

decodePdf.setPrintPageScalingMode(PrinterOptions.PAGE_SCALING_REDUCE_TO_PRINTER_MARGINS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM