我正在使用 Java Print Service API 打印到收据打印机。它打印得很好,但完成后不会自动剪切。如何让打印机自动裁切?
这是我用来打印的代码:
String defaultPrinter =
PrintServiceLookup.lookupDefaultPrintService().getName();
System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
InputStream is=null;
try {
printString+="\f";
System.out.println(printString);
is = new ByteArrayInputStream(printString.getBytes("UTF8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
try {
job.print(doc, pras);
} catch (PrintException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pjw.waitForDone();
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}