How to print barcode label with Zebra Printer

In C Sharp, Programming, Programming Projects by Baqir Ali

In order to print with Zebra Label printer, you should have the physical printer, as well as driver, installed then follow the following commands or you can download the completed project from our GitHub.

  • Create a new windows form project in visual studio Named Zebra Label Printer
  • Create the following form design
Printing zebra barcode label
  • Create a new class called “RawPrinterHelper.cs”
  • Add the following line
using System.Runtime.InteropServices;

Watch the following code closely and copy-paste the same code in the class

class RawPrinterHelper
    {
        // Structure and API declarions:
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class DOCINFOA
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDocName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pOutputFile;
            [MarshalAs(UnmanagedType.LPStr)]
            public string pDataType;
        }
        [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

        [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

        [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndDocPrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool StartPagePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool EndPagePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

        // SendBytesToPrinter()
        // When the function is given a printer name and an unmanaged array
        // of bytes, the function sends those bytes to the print queue.
        // Returns true on success, false on failure.
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            Int32 dwError = 0, dwWritten = 0;
            IntPtr hPrinter = new IntPtr(0);
            DOCINFOA di = new DOCINFOA();
            bool bSuccess = false; // Assume failure unless you specifically succeed.

            di.pDocName = "My C#.NET RAW Document";
            di.pDataType = "RAW";

            // Open the printer.
            if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                // Start a document.
                if (StartDocPrinter(hPrinter, 1, di))
                {
                    // Start a page.
                    if (StartPagePrinter(hPrinter))
                    {
                        // Write your bytes.
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }

        public static bool SendFileToPrinter(string szPrinterName, string szFileName)
        {
            // Open the file.
            FileStream fs = new FileStream(szFileName, FileMode.Open);
            // Create a BinaryReader on the file.
            BinaryReader br = new BinaryReader(fs);
            // Dim an array of bytes big enough to hold the file's contents.
            Byte[] bytes = new Byte[fs.Length];
            bool bSuccess = false;
            // Your unmanaged pointer.
            IntPtr pUnmanagedBytes = new IntPtr(0);
            int nLength;

            nLength = Convert.ToInt32(fs.Length);
            // Read the contents of the file into the array.
            bytes = br.ReadBytes(nLength);
            // Allocate some unmanaged memory for those bytes.
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.
            bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
            // Free the unmanaged memory that you allocated earlier.
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return bSuccess;
        }
        public static bool SendStringToPrinter(string szPrinterName, string szString)
        {
            IntPtr pBytes;
            Int32 dwCount;
            // How many characters are in the string?
            dwCount = szString.Length;
            // Assume that the printer is expecting ANSI text, and then convert
            // the string to ANSI text.
            pBytes = Marshal.StringToCoTaskMemAnsi(szString);
            // Send the converted ANSI string to the printer.
            SendBytesToPrinter(szPrinterName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
    }

  • Now go to the form and double click on the print to generate the click event
  • And code the following lines
private void button1_Click(object sender, EventArgs e)
{
try
            {
                string bName = txtBusinessName.Text;
                string supName = "CODEMODES";
                string pName = txtProductName.Text;
                string pCode = txtProductCode.Text;
                double price = double.Parse(txtProductPrice.Text);


                string s = "^XA ";
                //It shows image
                s += "^FO10,15^GFA,1764,1764,49,,::::::iR0G1CG7GEG0G3GFGDGEGFG…..8H0hN0G1GCH0GEW0G1GCP0G7U0hN0G1GEH0GEN0G1GBG8L0G3G8H0G1GFG8K0GFU0hO0GEG0G1GCN0G1GBG8L0GFG8H0G1GFG8J0G1GEU0hO0GFG0G3GCU0G1GFGEO0G7GFGCU0hO0G7HFG8U0G1GFG8O0G7GEV0hO0G1GFGEhL0,:::^FS";
                s += "^FO425,15^GFA,1764,1764,49,,::::::iR0G1G8gJ0……EGFG8H0hN0G1GCH0GEW0G1GCP0G7U0hN0G1GEH0GEN0G1GBG8L0G3G8H0G1GFG8K0GFU0hO0GEG0G1GCN0G1GBG8L0GFG8H0G1GFG8J0G1GEU0hO0GFG0G3GCU0G1GFGEO0G7GFGCU0hO0G7HFG8U0G1GFG8O0G7GEV0hO0G1GFGEhL0,:::^FS";
		//The above code shows image in byte format which is not complete put your own image code in the above string if required.
                s += "^LRY";
                s += "^FO0,55^GB399,35,35,B,0^FS";
                s += "^FO425,55^GB399,35,35,B,0^FS";
                s += "^CF9,22,10";
                s += "^FO0,65"; s += "^FB400,3,0,C,0^FD " + supName + " " + pName + " ^FS";
                //suplier name with product name
                s += "^FO415,65"; s += "^FB400,3,0,C,0^FD " + supName + " " + pName + " ^FS";
                s += "^LRN";
                s += "^BY2,2.0,60";
                s += "^FO10,100^A0N,20,20^BC^FD" + pCode + "^FS";   //product code
                s += "^FO430,100^A0N,20,20^BC^FD" + pCode + "^FS";
                s += "^CFA,15";
                s += "^FO230,100^GB150,85,3^FS";
                s += "^FO650,100^GB150,85,3^FS";
                s += "^CF0,25";
                s += "^FO240,115^FDRS. " + price + "^FS";  //price
                s += "^FO665,115^FD" + "RS. " + price + "^FS";
                s += "^CF0,25";
                s += "^FO240,150^FD" + "PCS." + 100 + "^FS"; //quantity available
                s += "^FO665,150^FD" + "PCS." + 100 + "^FS";
                s += "^XZ";

                RawPrinterHelper.SendStringToPrinter("PrinterName",s);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

}

The above code prints on TSC Printer for 50×25 1 up label, and you can loop over your list of products to print multiple labels at a time

How to generate my own label style

If you want to generate your own label style try labelry.com. First, design your label and dissect them according to your requirement.

Download the project from our GitHub page. Go to repository and search for a zebra label printer.

Support us by sharing this post

About the Author

Baqir Ali

Facebook Twitter

Hi, I am Baqir Ali ---- Blogger, Programmer, and a Teacher. I am currently working as a Programmer for Softnat Technologies. I have tons of ideas and data to share with you all via blogging.