java 指定打印机 进行打印
2014-12-11 00:49
459 查看
代码如下:
[java] view
plaincopy
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] view
plaincopy
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 指定打印机进行打印
- java 指定打印机 进行打印
- java 指定打印机 进行打印
- java指定打印机名称进行打印
- java 指定打印机 进行打印
- 有关于用java来调用打印机进行打印时的设置问题
- Java调用Acrobat Reader后台指定打印机打印PDF文件的方法
- lpr命令_Linux lpr 命令用法详解:将文件发送给指定打印机进行打印
- Java如何调用TSC打印机进行打印详解
- Java调用打印机打印指定路径图片
- java如何调用本地打印机进行图片打印
- java调用条码打印机指令进行打印
- java打印表格到打印机
- 给大家看一个可以web可以指定打印机的打印
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- 指定Web打印的打印机
- web开发,如何自动选定打印机进行打印
- Java--对于 class文件进行加密 然后通过我们自己写的类加载器进行加载指定类
- [转]Java 实现 POS 打印机无驱打印