选择相应的文件进行打印
JFileChooser fileChooser = new JFileChooser();
int state = fileChooser.showOpenDialog(null);
File file = null;
if(state == fileChooser.CANCEL_OPTION){
file = new File("c:/hello.doc");
}else{
file = fileChooser.getSelectedFile();
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
for(int i=0;i<printService.length;i++){
System.out.println(printService[i].getName());
}
PrintService PS = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(PS.getName());
/**
* printService :本地所有打印机列表
* PS :默认选择的打印机
* flavor :文件类型
* pras : 打印请求属性集
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, PS, flavor, pras);
if(service != null){
try {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(file);
Doc doc = new SimpleDoc(fis, flavor, null);
job.print(doc, pras);
System.out.println("打印成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("打印失败");
}
} }
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.LANDSCAPE);
MediaPrintableArea mp = new MediaPrintableArea(15f, 0f, 135f, 100f, Size2DSyntax.MM);
MediaSizeExp ms = new MediaSizeExp(140f, 105.5f,, Size2DSyntax.MM, MediaSizeName.INVOICE);
pras.add(ms.getMediaSizeName());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)