【问题标题】:How to print with win-1250 codepage on zebra printer?如何在斑马打印机上使用 win-1250 代码页进行打印?
【发布时间】:2011-01-12 14:34:12
【问题描述】:

我有此代码用于使用 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 是我从 here 获得的 Microsoft 课程。

我的问题是只有 ASCII 字符是这样打印的:

za     g  l  ja  

缺少非 ASCII 字符。

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

【问题讨论】:

  • 这听起来像是一个编码问题。您是否尝试过转换为其他编码,例如 UTF-8 或 ISO-8859-1?
  • 缺少的字符是来自 win-1250 或 iso-8859-2 字符集的波兰字符,我在 Encoding.Convert 方法中都尝试过...
  • 你能解决这个问题吗?同样的问题在这里。

标签: c# utf-8 zebra-printers zpl epl


【解决方案1】:

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

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

【讨论】:

  • 根据 epl 手动命令 I8,B,048 应该将 codepage 设置为 win-1250,它根本没有效果。我会和供应商讨论这个问题,但我有点希望其他一些不会说英语的开发人员也有同样的问题并以某种方式处理它;)
  • 顺便说一句。我有时会羡慕会说英语的开发人员;)如果没有所有编码噩梦,我的生活会容易得多;)
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    如果您需要为打印机添加自定义字符,请查看我为 SharpZebra 制作的 patch。修改它以添加对那些缺失字母的支持应该很简单。

    【讨论】:

      【解决方案4】:

      我在我的类中添加了一个辅助方法,它将字符串(默认为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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-15
        • 1970-01-01
        相关资源
        最近更新 更多