需要调用安装的虚拟打印机,这个虚拟打印机是将文档转成pdf格式,用到了javax.print,简单做了一个,但是不管是word\txt\jpg 什么格式的,转成的pdf都是空的,请各位看看,代码有什么不对的:private void printFile()throws Exception{
String filePath = "C:\\s.txt";
File file = new File(filePath);
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 默认打印机
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
if(service != null){
//创建打印
DocPrintJob job = service.createPrintJob();
//构造打印流
FileInputStream fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis,flavor,das);
job.print(doc, pras);
}
}
String filePath = "C:\\s.txt";
File file = new File(filePath);
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 默认打印机
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
if(service != null){
//创建打印
DocPrintJob job = service.createPrintJob();
//构造打印流
FileInputStream fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis,flavor,das);
job.print(doc, pras);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货