如何用Java调用打印机打印docx,PDF文档
展开全部
具体步骤如下:
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();
}
}
}
}
}
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();
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询