1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor,new HashPrintRequestAttributeSet());
PrintService service = null;
for( PrintService printService : printServices ) {
Attribute[] attrs = printService.getAttributes().toArray();
for (int j=0; j<attrs.length; j++) {
String attrName = attrs[j].getName();
String attrValue = attrs[j].toString();
if (attrName.equals("printer-name")){
if (attrValue.equals("Commercial")){
service = printService;
}
}
}
}
try {
if (service != null){
FileInputStream fis = new FileInputStream("c:/mytxt.txt");
Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
DocPrintJob printJob = service.createPrintJob();
printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
fis.close();
}
}
catch (Exception e1) {
System.out.println(e1.getMessage());
} |
Partager