print pdf417 barcode in zebra printer using c# and zpl

1k views Asked by At

I try to send a command to a Zebra printer. I use a RawPrinterHelper class and the SendStringToPrinter function but the proplem is that nothing is typing. I got the command from the zebra manual Here is my code:

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;
    }
}

and here i try to put my command

        var doc = new PrintDocument();
        doc.PrintPage += (s, e) => ZebraPrinter.PrintCoupon(Points, offerName, Description, Barcode, expDate, e);

        doc.PrinterSettings.PrinterName = "Zebra TTP 2030";
        doc.PrintController = new StandardPrintController();

        if (doc.PrinterSettings.IsValid)
        {
          doc.Print();
           string command = "^XA^BY2,3^FO10,10^B7N,5,5,,83,N^FDYourTextHere^FS^XZ";

            RawPrinterHelper.SendStringToPrinter(doc.PrinterSettings.PrinterName,command);
        }

}

but does not print nothing.

Any idea;

1

There are 1 answers

0
Jan Petter Jetmundsen On

I found this post as I am trying to interface a TTP-2030 myself. And I am starting to doubt that the 2030 actually supports ZPL. Maybe we are stuck with the KPL language.