最近公司做項目,用到條碼,對於打印機來說很簡單的啦,直接編輯文本輸出不就可以打印了嗎,是的,但是有時候,要打印的內容是動態的,需要制作條碼的軟件來完成有點不切實際。大多數的條碼打印機是不支持用指令直接打印漢字的。
網上找了好多,唯一可行的就是調用第三方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
時間
條碼
