Search code examples
c#imageprintingsdkzebra-printers

Printing with Zebra Link-Os SDK


I am using this code to print an image to a zebra printer.

ZebraPrinterConnection connection = new TcpPrinterConnection(ipAddress,port);
connection.Open();
ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
printer.GetGraphicsUtil().PrintImage("imageAddress");

It works fine but some times the printer doesn't print and in the code I dont get any errors. Is there a way to check if physically the label was printed?


Solution

  • There are a couple of ways to do this.

    First way is to check the status during/after you print and confirm there are no bytes in the buffer and no error on the printer:

    private boolean postPrintCheckPrinterStatus(Connection connection)
    {
        ZebraPrinter printer = ZebraPrinterFactory.getInstance(PrinterLanguage.ZPL, connection);
        PrinterStatus printerStatus = printer.getCurrentStatus();
    
        // loop while printing until print is complete or there is an error
        while ((printerStatus.numberOfFormatsInReceiveBuffer > 0) && (printerStatus.isReadyToPrint))
        {
            printerStatus = printer.getCurrentStatus();
        }
        if (printerStatus.isReadyToPrint) {
            System.out.println("Ready To Print");
            return true;
         } else if (printerStatus.isPaused) {
            System.out.println("Cannot Print because the printer is paused.");
         } else if (printerStatus.isHeadOpen) {
            System.out.println("Cannot Print because the printer head is open.");
         } else if (printerStatus.isPaperOut) {
            System.out.println("Cannot Print because the paper is out.");
         } else {
            System.out.println("Cannot Print.");
         }
        return false;
    }
    

    The other way to check this would be to use the printer odometer to ensure the label was printed. Check the odometer before and then again after:

    // Set settings, check status, print, etc.
            if (setPrintLanguage(statusConnection) && checkPrinterStatus(statusConnection))
            {
                int labelCount = Integer.parseInt(SGD.GET("odometer.total_label_count", statusConnection));
                // Send Print Job (1 label)
                String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
                printerConnection.write(zplData.getBytes());
    
                if (postPrintCheckPrinterStatus(statusConnection))
                {
                    int newLabelCount = Integer.parseInt(SGD.GET("odometer.total_label_count", statusConnection));
                    if (newLabelCount == labelCount + 1)
                    {
                        System.out.println("Print Successful.");
                    }
                }
                //else reprint?
            }