日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 指定打印机 进行打印

發布時間:2024/3/26 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 指定打印机 进行打印 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼如下:


[java]?view plain?copy
  • 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 指定打印机 进行打印的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。