JAVA 本地打印 DocFlavor、DocPrintJob job、PrintService
2016-06-24 16:50
591 查看
//=====================调用本地打印 1 打印 JasperPrint 文件 private void printPDF(JasperPrint jasperPrint){ DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); HashPrintServiceAttributeSet psat = new HashPrintServiceAttributeSet(); PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = ServiceUI.printDialog(null, 200, 200, pservices, defaultService, flavor, aset); if(service != null){ try { PrintServiceExporter exporter = new PrintServiceExporter(); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, aset); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, psat); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, false); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, false); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, service); exporter.setParameter(JRPrintServiceExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRPrintServiceExporterParameter.IGNORE_PAGE_MARGINS, true); exporter.exportReport(); } catch (Exception fe) { logger.info("打印失败", fe); } }else { logger.info("打印失败"); } } //=====================调用本地打印 2 根据字节数组打印 private void print(){ DocFlavor flavor=DocFlavor.INPUT_STREAM.JPEG; //get a printer PrintService[] printers=PrintServiceLookup.lookupPrintServices( flavor, null); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = ServiceUI.printDialog(null, 200, 200, pservices, defaultService, flavor, aset); //job DocPrintJob job=service.createPrintJob(); //document //已输出流进行打印 BufferedImage img=new BufferedImage( 400,300, BufferedImage.TYPE_USHORT_555_RGB ); Graphics g=img.getGraphics(); g.drawString(code, 100,100); ByteArrayOutputStream outstream=new ByteArrayOutputStream(); ImageIO.write( img, "jpg", outstream); byte[] buf=outstream.toByteArray(); InputStream stream=new ByteArrayInputStream(buf); Doc doc=new SimpleDoc(stream,flavor,null); // 根据文件路径进行打印 // FileInputStream fis = new FileInputStream("D:" + File.separator + "zkyzl.txt"); // DocAttributeSet das = new HashDocAttributeSet(); // Doc doc = new SimpleDoc(fis, flavor, das); //print job.print(doc, null); }
相关文章推荐
- jbpm4.4+Spring集成出现错误org.hibernate.HibernateException: jbpm.hibernate.cfg.xml not found
- Java - 基本类的使用(Map集合类)
- java之虚拟机详解
- Elasticsearch java API (10)Multi Get API
- 将用户在jsp表单输入的信息赋给JavaBean
- Java System类
- java 伪Random
- Stream API 例子
- jni javah
- Spring整合Struts 2,以及Spring中Bean的作用域问题
- Java环境搭建MyEclipse10+jdk1.8
- Java中split的用法
- 正则表达式在java中的运用
- Java实现的二分查找算法
- java项目配置jdbc.porperties时要注意jdbc.url的配置方式
- Elasticsearch java API (9)更新的API
- MyEclipse中配置JDK
- java调用dll库
- spring项目整合jedis及注意事项
- java 中的一些常用方法