用C#通过888-TT打印中文标签

        由于C#的String编码是unicode的(utf-16), 而888-TT则对中文字符认的是GBK,所以在打印前需要将utf16转为gbk.

private static byte[] utf16_gbk(string text)
        {
            //声明字符集   
            System.Text.Encoding utf16, gb2312;
            //utf16   
            utf16 = System.Text.Encoding.GetEncoding("UTF-16");
            //gb2312   
            gb2312 = System.Text.Encoding.GetEncoding("GBK");
            byte[] lvbytes = utf16.GetBytes(text);
            lvbytes = System.Text.Encoding.Convert(utf16, gb2312, lvbytes);
            return lvbytes;
        }

下面是rawprinterhelper的类, 通过它可以将原始内容直接输出至指定的打印机:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ZPLPrinter
{
    class RawPrinterHelper
    {
        // 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)]
        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);

        // 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)
        {
            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 = "PLM/ZeBra 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 you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }

        public static bool SendFileToPrinter(string szPrinterName, string szFileName)
        {
            // Open the file.
            FileStream fs = new FileStream(szFileName, FileMode.Open);
            // Create a BinaryReader on the file.
            BinaryReader br = new BinaryReader(fs);
            // Dim an array of bytes big enough to hold the file's contents.
            Byte[] bytes = new Byte[fs.Length];
            bool bSuccess = false;
            // Your unmanaged pointer.
            IntPtr pUnmanagedBytes = new IntPtr(0);
            int nLength;

            nLength = Convert.ToInt32(fs.Length);
            // Read the contents of the file into the array.
            bytes = br.ReadBytes(nLength);
            // Allocate some unmanaged memory for those bytes.
            pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
            // Copy the managed byte array into the unmanaged array.
            Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
            // Send the unmanaged bytes to the printer.
            bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
            // Free the unmanaged memory that you allocated earlier.
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return bSuccess;
        }
        public static bool SendUtf8StringToPrinter(string szPrinterName, string szString)
        {
            // by default System.String is UTF-16 / Unicode
            byte[] bytes = Encoding.Unicode.GetBytes(szString);

            // convert this to UTF-8. This is a lossy conversion and you might lose some chars
            bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytes);
            int bCount = bytes.Length;

            // allocate some unmanaged memory
            IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);

            // copy the byte[] into the memory pointer
            System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bCount);

            // Send the converted byte[] to the printer.
            SendBytesToPrinter(szPrinterName, ptr, bCount);

            // free the unmanaged memory
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);

            // it worked! Happy cry.
            return true;
        }
        public static bool SendBufferToPrinter(string szPrinterName, byte[] pvBuffer)
        {            
            int bCount = pvBuffer.Length;

            // allocate some unmanaged memory
            IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);

            // copy the byte[] into the memory pointer
            System.Runtime.InteropServices.Marshal.Copy(pvBuffer, 0, ptr, bCount);

            // Send the converted byte[] to the printer.
            SendBytesToPrinter(szPrinterName, ptr, bCount);

            // free the unmanaged memory
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);

            // it worked! Happy cry.
            return true;
        }

        public static bool SendStringToPrinter(string szPrinterName, string szString)
        {
            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 = Marshal.StringToCoTaskMemAnsi(szString);
            //pBytes = Marshal.StringToCoTaskMemUni(szString);

            // Send the converted ANSI string to the printer.
            SendBytesToPrinter(szPrinterName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
    }
}

下面是调用例子:

private void button2_Click(object sender, EventArgs e)
        {
            String lvPrinter = config.getValue("setting", "printer", "");
            String lvStr = "\nN\nA10,0,0,8,1,1,N,\"1不良现象不良现象不2\"\nP\n";
            byte[] lvBuffer = utf16_gbk(lvStr);
           // File.WriteAllBytes("d:\\temp\\a.txt", lvBuffer);
            RawPrinterHelper.SendBufferToPrinter(lvPrinter, lvBuffer);
        }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值