Java使用原生态的打印方法直接调用打印机打印,仅支持gif、jpeg、png
package com.example.demo.common;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class PrintUtils {
public static void main(String[] args) {
print("C:/Users/92384/Documents/test.png");
}
public static void print(String pathName){
File file = new File(pathName); //获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service + " supports :");
DocFlavor[] flavors = service.getSupportedDocFlavors();
for (int i = 0; i < flavors.length; i++) {
System.out.println("\t" + flavors[i]);
}
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//构造待打印的文件流
InputStream fis = null;
if(defaultService != null){
try {
DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das); //指定打印内容
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}