//设置打印属性 构造一个新的空打印请求属性集。
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(3));//打印份数,3份
//设置打印数据的格式DocFlavor.BYTE_ARRAY.PNG MIME 类型 = "image/png",打印数据表示形式类名 = "[B"(byte 数组)的 DocFlavor。
DocFlavor flavor = DocFlavor.BYTE_ARRAY.PNG;
//创建打印数据
Doc myDoc = new SimpleDoc(new File(""), flavor, null);
//查找所有符合条件的打印服务 lookupPrintServices(flavor, pras);查找能够打印指定 DocFlavor 的 PrintService。
PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, pras);
//将所有查找出来的打印机与自己想要的打印机进行匹配,找出自己想要的打印机
LookUpPrint p=new LookUpPrint();
PrintService myPrintService = p.GetPrintService("printName");
//可以输出打印机的各项属性
AttributeSet att = myPrintService.getAttributes();
for (Attribute a : att.toArray()) {
System.out.println("attributeName:"+a.getName()+ " attributeValue:" + att.get(a.getClass()).toString());
}
if (myPrintService != null) {
DocPrintJob job = myPrintService.createPrintJob();//创建文档打印作业
try {
job.print(myDoc, pras);//打印文档
} catch (Exception pe) {
pe.printStackTrace();
}
}else{
System.out.println("no printer services found");
}