如何在斑马打印机上使用 win-1250 代码页进行打印?

发布于 2024-10-11 12:00:34 字数 1242 浏览 3 评论 0 原文

我有用于使用 Zebra 打印机(具体为 RW 420)进行打印的代码

StringBuilder sb = new StringBuilder();            
sb.AppendLine("N");            
sb.AppendLine("q609");
sb.AppendLine("Q203,26");
//set printer character set to win-1250
sb.AppendLine("I8,B,001");
sb.AppendLine("A50,50,0,2,1,1,N,\"zażółć gęślą jaźń\"");
sb.AppendLine("P1");

printDialog1.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    byte[] bytes = Encoding.Unicode.GetBytes(sw.ToString());
    bytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(1250), bytes);                
    int bCount = bytes.Length;
    IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);
    System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bytes.Length);
    Common.RawPrinterHelper.SendBytesToPrinter(printDialog1.PrinterSettings.PrinterName, ptr, bCount);
}

RawPrinterHelper 是我从 此处

我的问题是只打印 ASCII 字符,如下所示:

za     g  l  ja  

丢失了非 ASCII 字符。

有趣的是,当我打开记事本并将相同的文本放入其中并在 Zebra 打印机上打印时,所有字符都正常。

I have this code for printing with Zebra printer (RW 420 to be specific)

StringBuilder sb = new StringBuilder();            
sb.AppendLine("N");            
sb.AppendLine("q609");
sb.AppendLine("Q203,26");
//set printer character set to win-1250
sb.AppendLine("I8,B,001");
sb.AppendLine("A50,50,0,2,1,1,N,\"zażółć gęślą jaźń\"");
sb.AppendLine("P1");

printDialog1.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    byte[] bytes = Encoding.Unicode.GetBytes(sw.ToString());
    bytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(1250), bytes);                
    int bCount = bytes.Length;
    IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);
    System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bytes.Length);
    Common.RawPrinterHelper.SendBytesToPrinter(printDialog1.PrinterSettings.PrinterName, ptr, bCount);
}

RawPrinterHelper is class from Microsoft that I got from here.

My problem is that only ASCII characters are printed like this:

za     g  l  ja  

Non-ASCII characters are missing.

Funny thing is that when I open Notepad and put the same text in there and print it on Zebra printer all characters are ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半岛未凉 2024-10-18 12:00:34

不同之处在于记事本使用打印机驱动程序,您绕过它。 Zebra 打印机支持使用其内置字体。它有代码页 950 的字符集以及所谓的“Latin 1”和“Latin 9”。关键问题是它们都不包含您需要的字形。打印机驱动程序通过向打印机发送图形而不是字符串来解决此问题。编程手册 在这里 顺便说一句。

我想这些打印机有某种安装额外字体的选项,如果不是这样的话,很难在世界其他地方进行销售。请联系您友好的打印机供应商以获取支持和选项。

The difference is that Notepad is using the printer driver, you are bypassing it. Zebra printers have some support for using its built-in fonts. It's got character sets for codepage 950 and something it calls "Latin 1" and "Latin 9". Key problem is that none of them contain the glyphs you need. The printer driver solves this problem by sending graphics to the printer, not strings. The programming manual is here btw.

I would imagine that these printers have some kind of option to install additional fonts, hard to make the sale in the rest of the world if that wouldn't be the case. Contact your friendly printer vendor for support and options.

岁月静好 2024-10-18 12:00:34

我发现 Wireshark 来自 ZebraDesigner 的字符集是 UTF-8,因此尝试将字符串转换为 byte[] 作为 utf-8

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sw.ToString());

像 ěščřžýáíé 这样的捷克字符现在可以了

I found with Wireshark that charset from ZebraDesigner is UTF-8 so try to convert string to byte[] as utf-8

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sw.ToString());

czech chars like as ěščřžýáíé is now OK

没有伤那来痛 2024-10-18 12:00:34

如果您需要向打印机添加自定义字符,请查看我制作的补丁对于夏普斑马。修改它以添加对那些缺失字母的支持应该很简单。

If you need to add custom characters to your printer, take a look at the patch I made for SharpZebra. It should be trivial to modify it to add support for those missing letters.

断念 2024-10-18 12:00:34

我在类中添加了一个辅助方法,该方法会将字符串(默认为 UTF-16)转换为 UTF-8 编码的 byte[] 然后打印它。

public static bool SendUtf8StringToPrinter(string szPrinterName, string szString)
{   
    // by default System.String is UTF-16 / Unicode
    byte[] bytes = Encoding.Unicode.GetBytes(szString);

    // convert this to UTF-8. This is a lossy conversion and you might lose some chars
    bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytes);
    int bCount = bytes.Length;

    // allocate some unmanaged memory
    IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);

    // copy the byte[] into the memory pointer
    System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bCount);

    // Send the converted byte[] to the printer.
    SendBytesToPrinter(szPrinterName, ptr, bCount);

    // free the unmanaged memory
    System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);

    // it worked! Happy cry.
    return true;
}

I added a helper method to my class that would convert a string (which is by default UTF-16) into a UTF-8 encoded byte[] and then print that.

public static bool SendUtf8StringToPrinter(string szPrinterName, string szString)
{   
    // by default System.String is UTF-16 / Unicode
    byte[] bytes = Encoding.Unicode.GetBytes(szString);

    // convert this to UTF-8. This is a lossy conversion and you might lose some chars
    bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytes);
    int bCount = bytes.Length;

    // allocate some unmanaged memory
    IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);

    // copy the byte[] into the memory pointer
    System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bCount);

    // Send the converted byte[] to the printer.
    SendBytesToPrinter(szPrinterName, ptr, bCount);

    // free the unmanaged memory
    System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);

    // it worked! Happy cry.
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文