JAVA 本地打印 DocFlavor、DocPrintJob job、PrintService

//=====================调用本地打印  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);
	}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值