none
Enviar código ZPL con C#

    Pregunta

  • Como dice la pregunta quisiera saber como enviar el código zebra ( ya lo tengo echo) por medio de un arreglo o string de C# a la impresora Zebra.
    lunes, 28 de enero de 2013 22:04

Todas las respuestas

  • Lo que yo hago es enviar la cadena de texto directo a la impresora con este metodo:

    public class PrintThroughDriver
        {
            // Structure and API declarions:
            [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
            internal class DOCINFOA
            {
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
                public string pDocName;
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
                public string pOutputFile;
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
                public string pDataType;
            }
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool OpenPrinter([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, long pd);
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool ClosePrinter(IntPtr hPrinter);
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [System.Runtime.InteropServices.In, System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStruct)] DOCINFOA di);
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool EndDocPrinter(IntPtr hPrinter);
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool StartPagePrinter(IntPtr hPrinter);
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool EndPagePrinter(IntPtr hPrinter);
            [System.Runtime.InteropServices.DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
            [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = false, ExactSpelling = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
            internal static extern Int32 GetLastError();
            // 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, string szDocumentName)
            {
                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 = szDocumentName;
                di.pDataType = "RAW";
                // Open the printer.
                if (OpenPrinter(szPrinterName, out hPrinter, 0))
                {
                    // 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 = GetLastError();
                    _erroCode = dwError;
                }
                return bSuccess;
            }
            public static bool SendStringToPrinter(string szPrinterName, string szString, string szDocumentName)
            {
                bool bResp;
                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 = System.Runtime.InteropServices.Marshal.StringToCoTaskMemAnsi(szString);
                // Send the converted ANSI string to the printer.
                bResp = SendBytesToPrinter(szPrinterName, pBytes, dwCount, szDocumentName);
                System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pBytes);
                return bResp;
            }
        }


    Jim Vázquez Castán

    lunes, 04 de febrero de 2013 23:42
  • Jim, has echo esto pasando el codigo ZPL por medio de una variable asp via web.
    martes, 05 de febrero de 2013 18:24
  • De hecho mi aplicacion es web pero el truco es que el nombre de la impresora es el nombre compartido en red, es decir, pongo la impresora compartida y le mando a la funcion el nombre. Algo asi: \\computadora\impresora 

    Jim Vázquez Castán

    lunes, 18 de febrero de 2013 18:10