关于使用条码打印机指令打印汉字的问题

最近公司做项目,用到条码,对于打印机来说很简单的啦,直接编辑文本输出不就可以打印了吗,是的,但是有时候,要打印的内容是动态的,需要制作条码的软件来完成有点不切实际。大多数的条码打印机是不支持用指令直接打印汉字的。

网上找了好多,唯一可行的就是调用第三方dll来完成,这样把要打印的汉字根据第三方的dll进行转换,得到了条码打印机ZPL自己识别的语言。就可行了。

我用的是斑马GK888T打印机。fnthex32.dll

 [DllImport("fnthex32.dll")]

        public static extern int GETFONTHEX(

                              string BarcodeText,

                              string FontName,

                              string FileName,

                              int Orient,

                              int Height,

                              int Width,

                              int IsBold,

                              int IsItalic,

                              StringBuilder ReturnBarcodeCMD);

 有个类BarcodePrint

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;



namespace Barcode

{

    class BarcodePrint

    {

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]

        private struct OVERLAPPED

        {

            int Internal;

            int InternalHigh;

            int Offset;

            int OffSetHigh;

            int hEvent;

        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped);

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        private static extern bool CloseHandle(int hObject);

        private int iHandle;

        public bool Open()

        {

            iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0);

            if (iHandle != -1)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        public bool Write(string Mystring)

        {

            if (iHandle != -1)

            {

                int i;

                OVERLAPPED x;

                byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);

                return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);

            }

            else

            {

                throw new Exception("LPT1端口未打开!");

            }

        }

        public bool Close()

        {

            return CloseHandle(iHandle);

        }

    }

}

 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 = "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 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 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);

            // Send the converted ANSI string to the printer.

            SendBytesToPrinter(szPrinterName, pBytes, dwCount);

            Marshal.FreeCoTaskMem(pBytes);

            return true;

        }

    }

}

 

当点击打印的时候

 string sBarCodeCMD = "";            //条码打印命令


//多行汉字打印
StringBuilder strbd1 = new StringBuilder(10240); StringBuilder strbd2= new StringBuilder(10240); StringBuilder strbd3 = new StringBuilder(10240); StringBuilder strbd4 = new StringBuilder(10240); int i1 = GETFONTHEX("打印的文字", "宋体", "temp1", 0, 30,20, 0, 0, strbd1); int i2 = GETFONTHEX("XXX:", "宋体", "temp2", 0, 20,15, 0, 0, strbd2); int i3 = GETFONTHEX("XX:", "宋体", "temp3", 0, 20,15, 0, 0, strbd3); int i4 = GETFONTHEX("XX:"+DateTime.Now+"", "宋体", "temp4", 0, 20, 15, 0, 0, strbd4); sBarCodeCMD = strbd1.ToString() + strbd2.ToString() + strbd3.ToString() + strbd4.ToString() + @"^XA^MD30 ^LH20,20^FO80,200^XGtemp1,1,1^FS ^LH20,20^FO80,240^GB620,0,2^FS ^LH20,20^FO80,260^XGtemp2,1,1^FS ^LH20,20^FO80,290^XGtemp3,1,1^FS ^LH20,20^FO80,320^GB620,0,2^FS ^LH20,20^FO80,350^BY1.5,3,100 ^BCN,100,Y,N,N^FD370921000001013090312024^FS ^LH20,20^FO80,480^XGtemp4,1,1^FS ^XZ"; RawPrinterHelper.SendStringToPrinter("ZDesigner GK888t (EPL)", sBarCodeCMD);

 打印后的效果大致如下:

注意这是code128,其实大多都差不多,只是指令不同而已。

打印的汉字

____________________________________

XXX

XX

时间

条码

你可能感兴趣的:(打印)