java 指定打印机打印_java 指定打印机 进行打印
代碼如下:
package com.printer;
import java.io.*;
import java.util.Locale;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
public class PrintTest {
public static void main(String args[]) {
FileInputStream psStream = null;
try {
psStream = new FileInputStream("D:\\work\\發票掃描件\\維修費20121025(發票聯).jpg");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
//設置打印數據的格式,此處為圖片gif格式
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.GIF;
//創建打印數據
//DocAttributeSet docAttr = new HashDocAttributeSet();//設置文檔屬性
//Doc myDoc = new SimpleDoc(psStream, psInFormat, docAttr);
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
//設置打印屬性
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(3));//打印份數,3份
//查找所有打印服務
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
//將所有查找出來的打印機與自己想要的打印機進行匹配,找出自己想要的打印機
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++) {
System.out.println("service found: " + services[i]);
String svcName = services[i].toString();
if (svcName.contains("Snagit 11")) {
myPrinter = services[i];
System.out.println("my printer found: " + svcName);
System.out.println("my printer found: " + myPrinter);
break;
}
}
//可以輸出打印機的各項屬性
AttributeSet att = myPrinter.getAttributes();
for (Attribute a : att.toArray()) {
String attributeName;
String attributeValue;
attributeName = a.getName();
attributeValue = att.get(a.getClass()).toString();
System.out.println(attributeName + " : " + attributeValue);
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();//創建文檔打印作業
try {
job.print(myDoc, aset);//打印文檔
} catch (Exception pe) {
pe.printStackTrace();
}
} else {
System.out.println("no printer services found");
}
}
}
總結
以上是生活随笔為你收集整理的java 指定打印机打印_java 指定打印机 进行打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 进口收货人备案系统官网(进口收货人备案系
- 下一篇: ddos攻击肉鸡价格(DDos攻击多少肉