博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 指定打印机 进行打印
阅读量:4131 次
发布时间:2019-05-25

本文共 1886 字,大约阅读时间需要 6 分钟。

代码如下:

[java] 
  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. }  

转载地址:http://zybvi.baihongyu.com/

你可能感兴趣的文章
CSS :hover和:active区别
查看>>
javascript callee 与 caller 的用法
查看>>
javascript Object.extend的用法
查看>>
HTML DOM innerheight、innerwidth 属性
查看>>
(转载)css单位px,em,rem,vw,vh,vmax,vmin
查看>>
(转载)浅谈Hybrid技术的设计与实现
查看>>
JavaScript indexOf() 方法
查看>>
JavaScript substr() 方法
查看>>
JavaScript slice() 方法
查看>>
JavaScript substring() 方法
查看>>
HTML 5 新的表单元素 datalist keygen output
查看>>
优雅降级 与 渐进增强
查看>>
HTML <fieldset> 标签
查看>>
HTML 5 <form> autocomplete 属性
查看>>
JavaScript Date 对象之 Date() getFullYear() getMonth()等
查看>>
HTML onfocus (获得焦点)和 onblur (失去焦点)
查看>>
JavaScript String 对象
查看>>
XML DOM Node 对象
查看>>
JS getComputedStyle() 与 currentStyle
查看>>
(转载) CSS height 和 line-height 的区别
查看>>