Mak*_*sk8 6 c# printing thermal-printer zebra-printers
我们刚刚从兄弟标签打印机迁移到 zebra,兄弟的 c# sdk 有点无聊,但它做了我想要它做的事情。基本上,它让我可以选择在设计器中创建标签,并将参考名称附加到文本文件、条形码文件等。但是,看着 zebra,我似乎找不到一种方法来做到这一点。我不喜欢这样的事实:您必须使用缺乏此功能的设计器来设计标签,或者 100% 在 C# 中完成,这会阻止我们的设计器稍后重新设计标签,又名硬编码标签是一种不行。
我一直在研究 neodynamic 的热敏标签,但对于这个小东西来说它的成本很高。
有我错过的解决方案吗?大量的谷歌搜索,甚至试图弄清楚如何通过 C# 将现有的兄弟标签打印到斑马打印机。这是可能的(也许),因为 p-touch 可以做到这一点,但我也认为 sdk 缺乏这个功能,这使得它无论如何都是不可能的。
在查看了兄弟 sdk 的更多选项后,我注意到我可以将标签作为 bmp 获取,可以轻松地将其打印到斑马标签打印机吗?
当我找到它时,我会添加源链接,但这就是我现在打印到 Zebra 的方式:
[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);
public static bool SendStringToPrinter(String printerName, String dataString)
{
int dwCount = (dataString.Length + 1) * Marshal.SystemMaxDBCSCharSize;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(dataString);
// Send the converted ANSI string to the printer.
SendPBytesToPrinter(printerName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
public static bool SendPBytesToPrinter(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 (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
//TODO: log error code
}
return bSuccess;
}
Run Code Online (Sandbox Code Playgroud)
其中 dataString 是打印机解释的 ZPL。