Zebra标签打印机c#SDK

时间:2015-05-27 19:43:12

标签: c# printing zebra-printers thermal-printer

我们刚刚从兄弟标签打印机转移到斑马线,而c#的兄弟sdk有点像meh但是它做了我想做的事情。基本上它给了我选择在设计器中创建标签并将引用名称附加到文本文件,条形码文件等。但是看看斑马我似乎找不到这样做的方法。我不喜欢你必须使用缺乏此功能的设计师设计标签,或者在c#中100%设计标签,这阻止我们的设计师在以后的路上重新设计标签,也就是硬编码标签是不去。

我一直在通过新的动力学方式来研究热敏标签,但是对于这个小东西来说,这需要付出很多代价。

那里有一个我错过的解决方案吗?吨和吨谷歌搜索甚至试图弄清楚如何通过c#打印现有的兄弟标签到斑马打印机。这是可能的(也许),因为p-touch可以做到,但我也认为sdk缺乏这个功能,这使得它无论如何都是不可能的。

在查看兄弟sdk以获得更多选项后,我注意到我可以将标签作为bmp,可以轻松打印到斑马标签打印机吗?

2 个答案:

答案 0 :(得分:2)

我会在以后找到它时添加源链接,但这就是我现在打印到Zebra的方式:

[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);


public static bool SendStringToPrinter(String printerName, String dataString)
        {
            int dwCount = (dataString.Length + 1) * Marshal.SystemMaxDBCSCharSize;

            // Assume that the printer is expecting ANSI text, and then convert
            // the string to ANSI text.
            IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(dataString);
            // Send the converted ANSI string to the printer.
            SendPBytesToPrinter(printerName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }


        public static bool SendPBytesToPrinter(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 (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
                //TODO: log error code
            }
            return bSuccess;
        }

其中dataString是打印机解释的ZPL。

答案 1 :(得分:0)

您可以将ZPL代码保存为简单的文本文件,然后使用shell命令在LPT1上移动它。

如果您的斑马线是通过网络插入的,最简单的方法/解决方法是使用" NET USE lpt1:\ [ip] [zebra] / persistent:yes

您可以使用Zebra Designer轻松生成ZPL,使用"打印到文件功能"