[ACCEPTED]-Sending a Barcode to a Zebra Printer from a Java Application-zebra-printers

Accepted answer
Score: 13

There are two ways to work with Zebra printers. The 13 first is to print as on regular printer. Java 12 printing basics are well explained in official tutorial. End 11 of page will treated by printer as end of 10 sticker. The disadvantage of this approach 9 is that all painting must be done by hands. I.e. you 8 can't use internal printer's barcoding ability.

The 7 second is to write ZPL commands directly 6 to printer. Something like this:

PrintService pservice = ... // acquire print service of your printer
DocPrintJob job = pservice.createPrintJob();  
String commands = "^XA\n\r^MNM\n\r^FO050,50\n\r^B8N,100,Y,N\n\r^FD1234567\n\r^FS\n\r^PQ3\n\r^XZ";
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(commands.getBytes(), flavor, null);
job.print(doc, null);

The disadvantage 5 is that you need to learn ZPL - Zebra Programming 4 Language. Although it is simple enough, but 3 such things as images and custom fonts could 2 make you headache. Programming manuals are 1 freely available on Zebra site: Part 1 and Part 2.

Score: 11

Not every Zebra printer has ZPL II, but 2 then you can use EPL

EPL2 programming guide for Zebra.pdf (Link in comment section)

Code 1 sample:

private static boolean printLabel(PrintService printService, String label) {
    if (printService == null || label == null) {
        System.err.println("[Print Label] print service or label is invalid.");
        return false;
    }
    String czas = new SimpleDateFormat("d MMMMM yyyy'r.' HH:mm s's.'").format(new Date());
    String command =  
            "N\n"+
            "A50,50,0,2,2,2,N,\""+label+"\"\n"+
            "B50,100,0,1,2,2,170,B,\""+label+"\"\n"+
            "A50,310,0,3,1,1,N,\""+czas+"\"\n"+
            "P1\n"
            ;
    
    byte[] data;
    data = command.getBytes(StandardCharsets.US_ASCII);
    Doc doc = new SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
    
    boolean result = false;
    try {
        printService.createPrintJob().print(doc, null);
        result = true;
    } catch (PrintException e) {
        e.printStackTrace();
    }
    return result;
}

More Related questions