怎么用java调用默认打印机打印文档?
发布网友
发布时间:2022-04-21 00:19
我来回答
共1个回答
热心网友
时间:2023-07-29 14:47
具体步骤如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.JFileChooser;
public class PrintDemo {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser(); // 创建打印作业
File file = new File("f:/111.txt"); // 获取选择的文件
// 构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
InputStream fis = 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 {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}