4

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.

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67

4 Answers4

6

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • according to epl manual command I8,B,048 should set codepage to win-1250 and it has no effect at all. I will talk to vendor about this, but I was kinda hoping that some other non-english speeking developer had the same problem and dealt with it some how ;) – Adrian Serafin Jan 13 '11 at 10:46
  • btw. I sometimes envy english speaking developers ;) my life would be a lot easier without all encoding nightmare ;) – Adrian Serafin Jan 13 '11 at 10:47
2

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

zdenál
  • 73
  • 7
1

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.

rkone
  • 136
  • 1
  • 4
0

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;
}
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92