示例#1
0
        private void PrintFormat(object sender, DoWorkEventArgs e)
        {
            Dictionary <int, string> formatVars = GetFormatVariables();

            try {
                OpenConnection();
                ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);

                string statusMessage = GetPrinterStatus(printer.GetCurrentStatus());
                if (statusMessage != null)
                {
                    errorMessage = "Printer Error: " + statusMessage + ". Please check your printer and try again.";
                }
                else
                {
                    if (!lastFormatOpenedSource.Equals(FORMAT_SOURCE_PRINTER))
                    {
                        printerConnection.Write(Encoding.UTF8.GetBytes(lastFormatOpenedContents));
                    }

                    printer.PrintStoredFormat(lastFormatOpened, formatVars, "UTF-8");
                    statusMessage = GetPrinterStatus(printer.GetCurrentStatus());
                    if (statusMessage != null)
                    {
                        errorMessage = "Printer Error after Printing: " + statusMessage + ". Please check your printer.";
                    }
                }
            } catch (ConnectionException error) {
                errorMessage = "Connection Error: " + error.Message;
            } finally {
                CloseConnection();
            }
        }
        /*
         * Print the labels using the supplied data using the zebra API
         */
        private void PrintLabels(LabelSetup l)
        {
            //Define the label format used to layout the label
            string labelFormat =
                $"^XA" +                                // Start the format
                $"^DFE:LABEL.ZPL^FS" +                  // Set the name of the format file
                $"^PW{l.pagewidth}" +                   // Set the pagewidth
                $"^A@N,{l.fontsize},E:TT0003M_.FNT" +   // Set the font and fontsize
                $"^FO0,{l.offset}" +                    // Set the starting position using the offset
                $"^FB{l.pagewidth},2,0,C" +             // Create a field block to center the text, max 2 lines
                $"^FN1\"Batch Code Text\"^FS" +         // Define the label text into variable slot 1
                $"^XZ";                                 // End the format

            //Send the label format to the printer
            printer.SendCommand(labelFormat);


            //Initialise a dictionary to pass variables to the printer
            Dictionary <int, string> vars = new Dictionary <int, string> {
            };

            //Print each label individually, providing a string for the label text via the dictionary
            for (int i = 0; i < l.quantity; i++)
            {
                if (cancel)
                {
                    break;
                }
                //Change the text based on the size of the label
                if (l.pagewidth < 200)
                {
                    vars[1]  = $"{l.batchcode}\\&";
                    vars[1] += $"{l.prefix}{l.startNumber + i}\\&";
                }
                else
                {
                    vars[1]  = $"{l.batchcode}\\&";
                    vars[1] += $"Bottle: {l.prefix}{l.startNumber + i}\\&";
                }

                //Print the label with associated text
                printer.PrintStoredFormat("E:LABEL.ZPL", vars);
            }
        }
示例#3
0
        /*        "E:T4FORM4.ZPL" Item Template
         * --------------------------------------------
         * Index|CharLimit|FieldName         | Example
         *   1  |    8    |"Item Count"      |"100/100"
         *   2  |   10    |"Service"         |"GrubHub"
         *   3  |   20    |"Customer Name"   |
         *   4  |   15    |"Label Name"      |
         *   5  |   40    |"Item Name"       |
         *   6  |    5    |"Size"            |"Large"
         *   7  |    3    |"Temperature"     |"Hot"
         *   8  |    6    |"Ice Level"       |"80% I"
         *   9  |    6    |"Sugar Level"     |"50% S"
         *   10 |   14    |"Milk Subsitution"|"Whole Milk Sub"
         *   11 |   40    |"Toppings"        |"Pearls, Pudding,"
         *   12 |   48    |"Special Instructions1"
         *   13 |   48    |"Special Instructions2"
         */
        public static bool PrintOrder(OrderContainer orderCon)
        {
            string[][] items = orderCon.OrderArray;

            bool printStatus = true;

            Debug.WriteLine("START: PrintOrder()");
            try {
                if (printerConn == null)
                {
                    //Get the connection to the printer driver
                    printerConn = FindConnection();
                    if (printerConn == null)
                    {
                        orderCon.PrintStatus = "Error: Printer not detected";
                        Debug.WriteLine("Error: Printer not detected");
                        return(false);
                    }
                }

                //We try to open the connection
                try {
                    printerConn.Open();
                } catch (Exception e) {
                    Debug.WriteLine("Error: Cannot open connection" + e.Message);
                    orderCon.PrintStatus = "Error: Cannot open connection";
                    return(false);
                }

                //The connection is open, so we get the instance of the printer and try to print
                try {
                    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, printerConn);
                    Debug.WriteLine("PrinterOrder() - got printer instance");

                    for (int i = items.Length - 1; i > -1; i--)
                    {
                        printer.PrintStoredFormat(PrintTemplatePath, items[i]);
                    }
                }catch (Exception e) {
                    Debug.WriteLine("Error: " + e.Message);
                    try {
                        //We check the status of the printer to see why it failed, which takes a long time without a UsbStatus channel
                        //which is why we only check if printing fails
                        string status = CheckPrinterStatus(printerConn);
                        if (status != "true")
                        {
                            Debug.WriteLine(status);
                            orderCon.PrintStatus = status;
                            return(false);
                        }
                    }catch (Exception e2) {
                        Debug.WriteLine("Error: " + e2.Message);
                        orderCon.PrintStatus = "Error: Connection error";
                        return(false);
                    }
                }finally {
                    printerConn.Close();
                }
            } catch (ConnectionException e) {
                Debug.WriteLine($"Error discovering local printers: {e.Message}");
                orderCon.PrintStatus = "Error: Can't discover local printers";
                printStatus          = false;
            }

            return(printStatus);
        }