gpt4 book ai didi

c# - 用于打印条码标签的 Zebra Printer C# 代码

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:23 25 4
gpt4 key购买 nike

这是我的 *.prn 文件:

I8,A,001 Q0001,0q831rNS5D10ZTJFOR20,0f100NB775,188,2,1,2,6,160,B,"SM00020000"X0,199,1,0,200P1

SM00020000 being the barcode.

string s = "I8,A,001\n\n\nQ0001,0\nq831\nrN\nS5\nD10\nZT\nJF\nO\nR20,0\nf100\nN\nB775,188,2,1,2,6,160,B,\"SM00020000\",199,1,0,200\nP1\n";

PrintDialog pd = new new PrintDialog();
pd.PrinterSettings = new System.Drawing.Printing.
pd.PrinterSettings.PrinterName = "ZDesigner GT800 (EPL)";
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);

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

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

此代码无法帮助我打印标签。文档进入打印队列,但之后没有任何反应。尽管打印机已正确配置并且我已使用 Zebra Designer 成功打印。

另外,我希望上面的代码能够在一行中打印 3 个标签,因为我的介质在一行中有 3 个标签。如何实现?

我的打印机型号是 ZDesigner GT800 (EPL)。

最佳答案

这是一个用于将字节发送到打印机的 Microsoft 类。开箱即用:

    using System;
using System.IO;
using System.Runtime.InteropServices;

public 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, int 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, int dwCount, out int 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, int dwCount)
{
int 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;
int dwCount;

// How many characters are in the string?
// Fix from Nicholas Piasecki:
// dwCount = szString.Length;
dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;

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

之后格式化您的 ZPL 请求并将其发送到打印类:

     StringBuilder ZplBuilder = new StringBuilder();

// Exemple ZPL String

ZplBuilder.Append("^XA"); //Start ZPL
ZplBuilder.Append("^FO320,42^APN,48,48^FD").Append(DateTime.Now.Date.ToString("dd/MM/yyyy")).Append("^FS");
ZplBuilder.Append("^FO0,304^GB720,168,1^FS");
ZplBuilder.Append("^FO0,306^GD720,166,1,B,L^FS");
ZplBuilder.Append("^FO0,306^GD720,166,1,B,R^FS");
ZplBuilder.Append("^XZ");
// End ZPL

string ZplString = ZplBuilder.ToString();

MemoryStream lmemStream = new MemoryStream();

StreamWriter lstreamWriter = new StreamWriter(lmemStream);
lstreamWriter.Write(ZplString);
lstreamWriter.Flush();
lmemStream.Position = 0;

byte[] byteArray = lmemStream.ToArray();

IntPtr cpUnmanagedBytes = new IntPtr(0);
int cnLength = byteArray.Length;
cpUnmanagedBytes = Marshal.AllocCoTaskMem(cnLength);
Marshal.Copy(byteArray, 0, cpUnmanagedBytes, cnLength);

RawPrinterHelper.SendBytesToPrinter("Intermec PC43d (203 dpi)", cpUnmanagedBytes, cnLength);
Marshal.FreeCoTaskMem(cpUnmanagedBytes);

它在带有 ZPL Protocel 的 intermec Printec 上运行良好。

希望对您有所帮助。

关于c# - 用于打印条码标签的 Zebra Printer C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com