My Java application for Windows prints on thermal printers via spooler without any interaction with a print dialog (using the code snippet below).
I don't know why, but it ignores the device configuration settings in the Printer Properties dialog box (on Windows 7, right-click on a printer in Printers and Devices). Settings for emitting buzzer beeps or cutting the paper at the end of the document are ignored.
There are proprietary ESC/POS codes which can be used to achieve the same effect but some of them work for 'direct protocol' printing only. I'm trying not to use them and configure the Printer Properties instead, in order to simplify troubleshooting printing issues (although I'd adopt direct protocol as last resort in this case).
How to make the print job accept the Printer Properties config settings? Or if necessary how to use direct protocol?
File file = File.createTempFile("order", "tmp");
try (PrintWriter printWriter = new PrintWriter(file)) {
printWriter.print(textToPrint);
}
DocPrintJob dpj = printService.createPrintJob();
InputStream stream = new ByteArrayInputStream(textToPrint.getBytes(StandardCharsets.UTF_8));
HashDocAttributeSet attributes = new HashDocAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(stream, flavor, attributes);
dpj.print(doc, new HashPrintRequestAttributeSet(new JobName("The Application", null)));
file.delete();