本文共 1886 字,大约阅读时间需要 6 分钟。
代码如下:
- 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;
- }
-
- DocFlavor psInFormat = DocFlavor.INPUT_STREAM.GIF;
-
-
-
- Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
-
-
- PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
- aset.add(new Copies(3));
-
-
- PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
-
-
-
- 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");
- }
- }
- }
转载地址:http://zybvi.baihongyu.com/