javaprintingcommandposjava-print

Java print API send commands to printer


I have got some printers well installed on my computer (Windows 7) and well displayed on the control panel.

I would like to send some special commands like: cut command, or barcode printing commands (ESC/POS commands).

Is it possible to do that using Java Print API ? or does Java Print API perform only printing services ?

Thanks in advance.


Solution

  • Problem solved : Thanks to VGR.

    here's a code to help anyone having same problem:

    private PrintService printer = ...; // init this using PrintService.lookupPrintServices();
    
    if(this.printer != null) {
            String commandToSend = "Some command\n";
    
            Doc myDoc = new SimpleDoc(commandToSend.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
            DocPrintJob job = this.printer.createPrintJob();
    
            try {
                job.print(myDoc, null);
            } catch (PrintException e) {
                e.printStackTrace();
            }
        }