- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的 *.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/
我将几个zpl文件发送到打印机进行测试,并且工作正常。 之后,我从UPS接收base64编码的zpl数据,并创建一个文件并通过Java小程序将其发送到打印机,现在我发送给它的任何zpl命令或文件都无济
我正在使用 Zebra GT800 标签打印机,标签角色大小为 2.5' * 1.75'。我正在努力使用 ZPL 命令将其调整为内部标签。我尝试使用在 ZPL 的用户指南中阅读的粗体文本中的几个值,但
如何为“文本”添加换行符?比如说: Hello World 会变成 Hello World 无论如何,我可以为我的代码添加一个换行符吗? ^FT78,76^A0N,28,28^FH\^FDHello\
我正在将 ZPL 代码传递给 Zebra 打印机。在这个 ZPL 代码中,我有一部分会生成二维码。 QR 码的值来 self 在为 WM6.5 开发的移动应用程序中使用条形码扫描功能获得的字符串。扫描
我正在将 ZPL 代码传递给 Zebra 打印机。在这个 ZPL 代码中,我有一部分会生成二维码。 QR 码的值来 self 在为 WM6.5 开发的移动应用程序中使用条形码扫描功能获得的字符串。扫描
我在 ZPL 中编程,我需要手动定义一个变量。一个简单的 Hello World 示例: ^XA ^FO,20,10^ADN,40,25^FDHello World^FS ^XZ 我希望能够: SET
我正在尝试在我的 Zebra Z410 标签打印机上打印波斯字符。但由于某种原因,它正在打印 ?????。我发送到打印机的 ZPL 代码是 ^XA ^FO50,50 ^PA1,1,1,1 ^A@N,5
有没有办法计算给定文本的 Zebra Font 0 的总宽度?考虑以下 ZPL 命令, **^XA^FO100,150^A030,30^FDSample Text^FS^XZ** 这里字符的高度和宽度
使用带有对特定标签进行编码的 ZPL 代码的 Zebra 打印机,我如何将 a 添加到现有的 ZPL 命令,以便打印同一标签的 3 个副本。 这必须是 ZPL 代码的一部分,因为发送命令 3 次不是一
我开发了一个在 Zebra TC70 上运行的应用程序。主要原因是该设备内置了二维条码扫描仪。 现在我还想在应用程序商店中发布相同的应用程序,而不需要 Zebra 要求。我内置了一项检查,用于检查设备
基于this question我已经实现了以下代码来将直接命令发送到我的 Zebra TLP2844 var cmm: AnsiString; i: integer; begin comm
为什么我的代码不能在本地主机上运行?用户打开日历并选择日期后,应该会弹出一条警报消息,但这并没有发生。它正在 jsfiddle.net 上运行。 Zebra_DatePi
我需要在 Zebra zq520 蓝牙热敏打印机上添加打印功能。我设法使用 flutter_blue 插件来做到这一点,但我对实现并不满意。我讨厌将字符串分成更小的 block 以便通过蓝牙(并且等待
我需要在 Zebra zq520 蓝牙热敏打印机上添加打印功能。我设法使用 flutter_blue 插件来做到这一点,但我对实现并不满意。我讨厌将字符串分成更小的 block 以便通过蓝牙(并且等待
我对 tablesorter 插件和 zebra 小部件有疑问,它们为偶数/奇数行启用不同的样式。 我的页面以一个空表开始;然后,用户编译一个表单,然后加载我的表格...所以问题是:在初始加载(也对列
我需要将 Zebra M4Plus 打印机中的“日期字段”从 UTF-8 编码为 UTF-16。为此,我需要使用“所需的翻译表”。在文档中我发现了这个: ~DER:JIS.DAT,27848,3000
任何熟悉 Zebra Datepicker 的人? 我似乎无法显示日历图标。 http://jsfiddle.net/abalter/gzbqrxpL/ 我也在尝试: Unt
我正在尝试使用 Zebra QLn220 和 link_os_sdk 从 iOS 设备打印图像。图像为 719x1248 像素,但打印时图像总是被切掉一半。我已通过 Zebra 设置实用程序将打印机的
过滤结果后显示“斑马”列表时出现问题。我有一个显示完美的斑马列表,这里是代码: $('ul li:odd').addClass('zebra_odd'); $('ul li:even').addCla
我有斑马 GC420d。使用 zebra 0.0.3a ,这是我的问题的一个例子: label = """ ^XA ^FO10,10 ^A0,40,40 ^FD Hello World ^FS ^XZ
我是一名优秀的程序员,十分优秀!