4

我有使用 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 打印机上打印时,所有字符都可以。

4

4 回答 4

6

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

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

于 2011-01-12T16:16:59.317 回答
2

我在 Wireshark 中发现 ZebraDesigner 的字符集是 UTF-8,所以尝试将字符串转换为 byte[] 作为 utf-8

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

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

于 2015-07-01T09:30:24.047 回答
1

如果您需要向打印机添加自定义字符,请查看我为 SharpZebra 制作的补丁。修改它以添加对那些丢失字母的支持应该是微不足道的。

于 2011-02-21T17:28:18.553 回答
0

我在我的类中添加了一个辅助方法,它将一个字符串(默认情况下是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;
}
于 2017-03-21T07:54:40.257 回答