使用c#和zpl在zebra打印机中打印pdf417条形码

时间:2015-06-25 11:11:36

标签: c# printing zebra-printers zpl

我尝试向Zebra打印机发送命令。我使用RawPrinterHelper类 和SendStringToPrinter函数,但问题是什么都没有输入。我从斑马手册中得到了命令这是我的代码:

// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

在这里我试着把我的命令

public class RawPrinterHelper
{
    [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,
        Int32 pDefault);


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

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterW", SetLastError = true, CharSet = CharSet.Unicode,
        ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, int level, ref DOCINFOW pDI);

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

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

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

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

    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount)
    {
        var hPrinter = (IntPtr)(0);
        int dwError; // Last error - in case there was trouble.
        var di = new DOCINFOW();
        int dwWritten = 0;

        // Set up the DOCINFO structure.
        di.pDocName = "My Visual Basic .NET RAW Document";
        di.pDataType = "RAW";
        // Assume failure unless you specifically succeed.
        bool bSuccess = false;
        if (OpenPrinter(szPrinterName, out hPrinter, 0))
        {
            if (StartDocPrinter(hPrinter, 1, ref di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    // Write your printer-specific bytes to the printer.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }

        if (bSuccess == false)
        {
            Marshal.GetLastWin32Error();
        }
        return bSuccess;
    }

    public static bool SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        var fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        var br = new BinaryReader(fs);
        // Dim an array of bytes large enough to hold the file's contents.

        byte[] bytes = br.ReadBytes((int)fs.Length);

        IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem((int)fs.Length);

        Marshal.Copy(bytes, 0, pUnmanagedBytes, (int)fs.Length);

        bool bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, (int)fs.Length);

        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }


    public static object SendStringToPrinter(string szPrinterName, string szString)
    {

        int dwCount = szString.Length;

        IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString);

        SendBytesToPrinter(szPrinterName, pBytes, dwCount);
        Marshal.FreeCoTaskMem(pBytes);
        return null;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct DOCINFOW
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pOutputFile;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDataType;
    }
}

}

但不打印任何内容。

任何想法;

1 个答案:

答案 0 :(得分:0)

我发现这篇文章,因为我试图自己连接TTP-2030。我开始怀疑2030实际上支持ZPL。也许我们坚持使用KPL语言。