java 指定打印机 进行打印

代码如下:


[java]  view plain copy
  1. package com.printer;  
  2.   
  3. import java.io.*;  
  4. import java.util.Locale;  
  5.   
  6. import javax.print.*;  
  7. import javax.print.attribute.*;  
  8. import javax.print.attribute.standard.*;  
  9.   
  10. public class PrintTest {  
  11.   
  12.     public static void main(String args[]) {  
  13.   
  14.         FileInputStream psStream = null;  
  15.         try {  
  16.             psStream = new FileInputStream("D:\\work\\发票扫描件\\维修费20121025(发票联).jpg");  
  17.         } catch (FileNotFoundException ffne) {  
  18.             ffne.printStackTrace();  
  19.         }  
  20.         if (psStream == null) {  
  21.             return;  
  22.         }  
  23.         //设置打印数据的格式,此处为图片gif格式  
  24.         DocFlavor psInFormat = DocFlavor.INPUT_STREAM.GIF;  
  25.         //创建打印数据  
  26. //      DocAttributeSet docAttr = new HashDocAttributeSet();//设置文档属性  
  27. //      Doc myDoc = new SimpleDoc(psStream, psInFormat, docAttr);  
  28.         Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
  29.           
  30.         //设置打印属性  
  31.         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();  
  32.         aset.add(new Copies(3));//打印份数,3份  
  33.           
  34.         //查找所有打印服务  
  35.         PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);  
  36.   
  37.         // this step is necessary because I have several printers configured  
  38.         //将所有查找出来的打印机与自己想要的打印机进行匹配,找出自己想要的打印机  
  39.         PrintService myPrinter = null;  
  40.         for (int i = 0; i < services.length; i++) {  
  41.             System.out.println("service found: " + services[i]);  
  42.             String svcName = services[i].toString();  
  43.             if (svcName.contains("Snagit 11")) {  
  44.                 myPrinter = services[i];  
  45.                 System.out.println("my printer found: " + svcName);  
  46.                 System.out.println("my printer found: " + myPrinter);  
  47.                 break;  
  48.             }  
  49.         }  
  50.   
  51.         //可以输出打印机的各项属性  
  52.         AttributeSet att = myPrinter.getAttributes();  
  53.   
  54.         for (Attribute a : att.toArray()) {  
  55.   
  56.             String attributeName;  
  57.             String attributeValue;  
  58.   
  59.             attributeName = a.getName();  
  60.             attributeValue = att.get(a.getClass()).toString();  
  61.   
  62.             System.out.println(attributeName + " : " + attributeValue);  
  63.         }  
  64.   
  65.         if (myPrinter != null) {  
  66.             DocPrintJob job = myPrinter.createPrintJob();//创建文档打印作业  
  67.             try {  
  68.                 job.print(myDoc, aset);//打印文档  
  69.   
  70.             } catch (Exception pe) {  
  71.                 pe.printStackTrace();  
  72.             }  
  73.         } else {  
  74.             System.out.println("no printer services found");  
  75.         }  
  76.     }  
  77. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值