How to print with win-1250 code page on a zebra printer?

I have this code for printing on a Zebra printer (specific 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 is a class from Microsoft that I got from here .

My problem is that only ASCII characters are printed as follows:

 za gl ja 

Missing ASCII characters.

It's funny that when I open Notepad and put the same text there and print it on a Zebra printer, all the characters are in order.

+4
source share
4 answers

The difference is that Notepad uses the printer driver, you bypass it. Zebra printers have some support for using embedded fonts. He got a character set for codepage 950 and something that he calls "Latin 1" and "Latin 9". The main problem is that none of them contain the glyphs you need. The printer driver solves this problem by sending graphics to the printer, not the lines. Programming Guide is here .

I would suggest that these printers have some kind of option to install additional fonts that are difficult to sell in the rest of the world if this is not the case. Contact your friendly printer vendor for support and options.

+6
source

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

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

Czech characters like ěščřžýáíé are now OK

+2
source

If you need to add custom characters to your printer, check out the patch I made for SharpZebra. It must be trivial to modify it to add support for those missing letters.

+1
source

I added a helper method for my class that converts a string (default is UTF-16 ) to UTF-8 encoded byte[] and then prints it.

 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; } 
0
source

Source: https://habr.com/ru/post/1335256/


All Articles