print *.lbl file

Jun 3, 2014 at 4:24 PM
Dear all,

Is possible print the lbl file (made with Zebra Design 2) in C#?
It's possible, how can be done?

Regards,
Toni
Coordinator
Jun 3, 2014 at 4:41 PM
Yes, you can use c# to print a lbl file. There are different methods depending on how your printer is attached. If you download the source you can see how the sharpzebra library communicates with the printer in the Printing subfolder.
Jun 3, 2014 at 4:52 PM
How to make it?
Now I'm read de lbl file:
       using (BinaryReader b = new BinaryReader(File.Open(@"C:\Program Files (x86)\Zebra Technologies\ZebraDesigner 2\Samples\Labels\sample1.lbl", FileMode.Open)))
        {
            try
            {
                int length = (int)b.BaseStream.Length;
                byte[] allData = b.ReadBytes(length);

                using (BinaryWriter w = new BinaryWriter(File.Open(@"C:\Temp\TESTnew.lbl", FileMode.Create)))
                {
                    for (int i = 0; i < length; i++)
                    {
                        MessageBox.Show(w.BaseStream.ToString());
                        w.Write(allData[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
How to change values in lbl file? how to print it?

Regards,
Toni
Coordinator
Jun 3, 2014 at 5:09 PM
If you need to change it, you'll have to learn ZPL/EPL/CPL, whichever format the label is saved in.


Here's a cut and paste of the spool printer code for printing to a printer installed into windows. Just use Print(allData, "My Zebra Printer") to use it.
 public void Print(byte[] data, string printername)
        {
            Int32 dwCount;
            dwCount = data.Length;
            GCHandle h = GCHandle.Alloc(data, GCHandleType.Pinned);
            SendBytesToPrinter(printername, h.AddrOfPinnedObject(), dwCount);
            h.Free();            
        }


        // 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)]
        private 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)]
        private static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private 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)]
        private static extern bool EndDocPrinter(IntPtr hPrinter);

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

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

        [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private 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.
        private 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 = "Labels";
            di.pDataType = "RAW";

            if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                if (StartDocPrinter(hPrinter, 1, di))
                {
                    if (StartPagePrinter(hPrinter))
                    {
                        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;
        }                
Jun 3, 2014 at 8:56 PM
very, very thanks rkone...

Its not possible change the values ​​of variables lbl files?

Regards