ZPL some simple instructions

^ XA start

^ XZ end

^LL Set the label length (I am using TSC TE344 printer, this seems to have no effect)

^PW Sets the label width

^ the FS newline

^ the FS annotation

^LH0,0 Margin of paper, left margin and top margin

^FO0,0 Sets the print coordinate

^FD123 Sets the print content, which can be text, one-dimensional code, two-dimensional code

For more instructions: see here

A complete ZPL directive

^XA^ XA^LH0,0^LL100^PW580 ^FX Text ^FO10,10^AFN,0,20,10^FD123456^FS ^FX One-dimensional code ^FO10,10^BY2,11.0,10,40^BCC,90,N^FD12345678^FS FX qr code ^FO10,10^BQ,2,5^CI26^FH^FDQA^FD1215445244^FS ^XZCopy the code

Print text

, 10 ^ ^ FO10 AFN, 0, 10 ^ FD123456 ^ FSCopy the code

Print one dimensional code

10 ^ ^ FO10, BY2, 11.0, 10 40 ^ BCC, 80, N ^ FD12345678 ^ FSCopy the code

^BCC,80, 80 of N is the height of one dimension code. N indicates that if no comment line is displayed, 12345678 is displayed

Print qr code

10 ^ ^ FO10, BQ, 2, 5 ^ CI26 ^ FH ^ FDQA ^ FD1215445244 ^ FSCopy the code

^BQ,2,5 of 5 is the size of the two-dimensional code, ranging from 1 to 10

About Chinese Printing

Because the printer does not have Chinese character library, printing Chinese will appear garbled

One solution is to install Chinese characters, code table, this is a bit of trouble, do not need

The other is to convert text into printer instructions via fnthex32.dll and send them to the printer

Fnthex32. DLL download:

Link: pan.baidu.com/s/1_lU0zswv… Extraction code: OI41

use

[DllImport(" fnthex32.dll ")] private static extern int GETFONTHEX(string text,// text string fontName,// font string TempName,// temporary variable int Orient,// direction int height,// font height int width,// font width int isBold,// whether to bold 0,1 int isItalic,// whether to italic 0,1 StringBuilder cmd); / / content / / / < summary > / / / get the text encoding / / / < / summary > / / / < param name = "text" > text < param > / / / < param name = "fontName" > font < param > / / / <param name="tempName"> temporary variable </param> // <param name="height"> font height </param> // <param name="width"> font width </param> /// <param name="isBlod"> Whether bold </param> // <param name="isItalic"> Whether italic </param> // <returns></ RETURNS > public static String GetFontHex(string text, string fontName, string tempName, int height = 40, int width = 15, bool isBlod = false, bool isItalic = false) { int nIsBlod = isBlod ? 1:0; int nIsItalic = isItalic ? 1:0; StringBuilder context = new StringBuilder(text.Length * 1024); int count = GETFONTHEX(text, fontName, tempName, 1, height, width, nIsBlod, nIsItalic, context); string temp = " " + context.ToString(); temp = temp.Substring(0, count); return temp; }Copy the code

Get the ZPL directive with GetFontHex

String sTemp = GetFontHex(" ah ", "Arial", "temp0");Copy the code

Where temp0 is a temporary variable

Next, it is set with an instruction

, 0 ^ ^ FO30 XGtemp0, 1, 1 ^ FSCopy the code

Complete instructions

, 0 ^ ^ XA ^ LH0 LL80 ^ PW580 ~ DGtemp0, 00240006, iH0108H0CL01FDHFEJ03F918018J031918018J03191H018J03191H018J031 93H018J03192H198J031921F98J03192H198J03194H198J03192H198J0319I198J0319I198J03191 9198J031909198J03190D198J03190DF98J03F90D198J031919H18J03H1F8018J03013H018L01I01 8 l01i018l01i018l01i018l01i018l01h01f8l01i07m01i02ik0 ^ FO30, 0 ^ XGtemp0, 1, 1 ^ FS ^ XZCopy the code

About the printing of pictures

To print an image, you need to convert the image into a hexadecimal command

Blog.csdn.net/weixin_3821…

The instruction is sent to the printer

Printer sharing is preferred

The system.io.File.Copy method is used

Using (system.io.StreamWriter sw = new system.io.StreamWriter(printFilePath)) {sw.write (cmd.tostring ()); } // Send the ZPL directive to the printer file. Copy(printFilePath, mPrintName, true);Copy the code

Among them

PrintFilePath is the ZPL command file path

MPrintName is the printer path, for example: mPrintName = @”\\192.168.0.132\TSC TE344″;

The instruction needs to get the local IP4 address

// </summary> private string GetIP4() {string IP4 = string.empty; IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] ips = ipe.AddressList; for (int i = 0; i < ips.Length; i++) { if (ips[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { ip4 = ips[i].ToString(); break; } } return ip4; }Copy the code

Instead, write 127.0.0.1 as the local IP address

About printer initialization

Because before using Jiabo Gprinter GP-9134T printer, not using ZPL instruction, directly use PrintDocument class print control pictures, printers are automatic calibration, now TSC TE344 printer, because now the printer does not have calibration function, Printing with PrintDocument will always print half of the paper, so it is changed to ZPL instruction printing, in order to use a variety of printers at the same time, and then do compatibility.

Since PrintDocument is used to print, the paper is not accurate, so we can initialize the printer with the following instruction, which is obtained from NiceLabel

First, you set the paper size, margins, etc. When you print the file, you can get the initializing order.

Private void PrintInit() {string printerInitCMD = @" < XPML ><page quantity='0' Pitch =' 15.1mm '></ XPML >SIZE 47.5 mm, 15.1mm GAP 2 mm, 0 mm DIRECTION 0,0 REFERENCE 0,0 OFFSET 0 mm SET PEEL OFFSET CUTTER OFF < XPML ></ PAGE ></ XPML >< PAGE quantity='1' pitch=' 15.1mm '></ XPML >SET TEAR ON CLS <xpml></page></xpml><xpml><end/></xpml>"; string PrinterInitFilePath = printPath + "\\PrinterInit.prn"; using (StreamWriter sw = new StreamWriter(PrinterInitFilePath)) { sw.Write(printerInitCMD); } File.Copy(PrinterInitFilePath, mPrintName, true); }Copy the code

Type paper specification used

Double row printing paper

Actual size (mm)

Label width and height: 22*15

Wide: 50

Left margin: 2

Leading: 2

Column spacing: 2

Size in code

Label width and height: 235 x 160

Width of paper: 580

Left and right margin: 30

Middle seam margin: 50

Conclusion:

Since I am new to ZPL and not familiar with it, the project needs to support the function of custom printing templates, that is, users can adjust and drag label positions by themselves.

Considering the

1.ZPL preview is a bit difficult

2. Parameter limitations of ZPL instruction, such as bar code width and height cannot be compatible with the size of the control

So by printing the picture directly on the control, converting the picture into a hexadecimal command and sending it to the printer,

Another advantage is that you don’t have to worry about Chinese garbled characters

So here’s my PrintManager

Private static extern int GETFONTHEX(string) private static extern int GETFONTHEX(string) private static extern int GETFONTHEX(string Text,// text string fontName,// font string tempName,// temporary variable int Orient,// direction int height,// font height,// font width,// font width,// IsBold,// whether to bold 0,1 int isItalic,// whether to italic 0,1 StringBuilder CMD); / / content / / / < summary > / / / get the text encoding / / / < / summary > / / / < param name = "text" > text < param > / / / < param name = "fontName" > font < param > / / / <param name="tempName"> temporary variable </param> // <param name="height"> font height </param> // <param name="width"> font width </param> /// <param name="isBlod"> Whether bold </param> // <param name="isItalic"> Whether italic </param> // <returns></ RETURNS > public static String GetFontHex(string text, string fontName, string tempName, int height = 40, int width = 15, bool isBlod = false, bool isItalic = false) { int nIsBlod = isBlod ? 1:0; int nIsItalic = isItalic ? 1:0; StringBuilder context = new StringBuilder(text.Length * 1024); int count = GETFONTHEX(text, fontName, tempName, 1, height, width, nIsBlod, nIsItalic, context); string temp = " " + context.ToString(); temp = temp.Substring(0, count); return temp; } #endregion int paperWidth = 580; // left and right margin int padding = 30; // int middlePadding = 50; Int labelWidth = 235; // labelHeight int labelHeight = 160; string ip4; public StringBuilder cmd; // Print directory string printPath; // Save the print instruction string printFilePath; // Printer name private string mPrintName; public PrintManager() { printPath = Application.StartupPath + "\\Print"; if (Directory.Exists(printPath) == false) { Directory.CreateDirectory(printPath); } printFilePath = printPath + "\\print.prn"; ip4 = GetIP4(); } public void Init(string printerName) { mPrintName = string.Format(@"\\{0}\{1}", ip4, printerName); / / mPrintName = @ \ \ "192.168.0.132 \ TSC TE344"; // The printer initializes PrintInit(); cmd = new StringBuilder(); CMD. AppendFormat (@ "^ XA ^ LH0, 0 ^ LL {0} ^ PW {1}", 80, paperWidth); // </summary> private string GetIP4() {string IP4 = string.empty; IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] ips = ipe.AddressList; for (int i = 0; i < ips.Length; i++) { if (ips[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { ip4 = ips[i].ToString(); break; } } return ip4; } private void PrintInit() {string printerInitCMD = @" < XPML ><page quantity='0' Pitch =' 15.1mm '></ XPML >SIZE 47.5 mm, 15.1mm GAP 2 mm, 0 mm DIRECTION 0,0 REFERENCE 0,0 OFFSET 0 mm SET PEEL OFFSET CUTTER OFF < XPML ></ PAGE ></ XPML >< PAGE quantity='1' pitch=' 15.1mm '></ XPML >SET TEAR ON CLS <xpml></page></xpml><xpml><end/></xpml>"; string PrinterInitFilePath = printPath + "\\PrinterInit.prn"; using (StreamWriter sw = new StreamWriter(PrinterInitFilePath)) { sw.Write(printerInitCMD); } File.Copy(PrinterInitFilePath, mPrintName, true); } / / / < summary > / / / Print / / / < summary > public void Print () {CMD. Append (" ^ XZ "); Using (system.io.StreamWriter sw = new system.io.StreamWriter(printFilePath)) {sw.write (cmd.tostring ()); } // Send the ZPL directive to the printer file. Copy(printFilePath, mPrintName, true); } /// </summary> public void SetText(string value, AppendFormat("^FO{0},{1}^AFN,0,20,10^FD{2}^FS", padding + loc.x, loc.y, value); } int iTemp = 0; // </summary> public void SetText_Chinese(string value, Point loc) { string tempName = "temp" + iTemp; string sTemp = GetFontHex(value, "Arial", tempName); cmd.Append(sTemp); CMD. AppendFormat (" ^ FO {0}, {1} ^ XG {2}, 1, 1 ^ FS ", padding + loc. X, loc. Y, tempName); iTemp++; } /// </summary> public void SetQR_Code(string value, Point loc, AppendFormat("^FO{0},{1}^BQ,2,{2}^CI26^FH^FDQA^FD{3}^FS", padding + loc.X, loc.Y, size, value); } /// </summary> public void SetCODE_128(string value, Point loc, Size Size) {CMD. AppendFormat (@ "^ FO {0}, {1} ^ BY2, 11.0, 60, {2} ^ BCC, {3}, N ^ FD {4} ^ FS", padding + loc. X, loc. J Y, Size, Width, size.Height, value); } int iTemp2 = 0; // </summary> public void SetImage(Image Image, bool isRight = false) { image = ReSizeImage(image, new Size(labelWidth + 20, labelHeight)); string data = ZebraUnity.BitmapToHex(image, out int to, out int row); string zpl = string.Format("~DGR:Temp{0}.GRF,{1},{2},{3}", iTemp2, to, row, data); cmd.Append(zpl); int offset = isRight == true ? labelWidth + middlePadding : 0; Cmd. AppendFormat("^FO{0},0^XGR:Temp{1}.GRF,1,1^FS", padding + offset, iTemp2); iTemp2++; Public static Image ReSizeImage(Image img, Image ReSizeImage) Size size) { Bitmap bitmap = new Bitmap(size.Width, size.Height); Graphics g = Graphics.FromImage(bitmap); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(img, 0, 0, bitmap.Width, bitmap.Height); g.Dispose(); return bitmap; }}Copy the code

ZebraUnity.BitmapToHex see blog.csdn.net/weixin_3821…

Related links:

ZPL instruction parsing:

Baidu library: wenku.baidu.com/view/ec78b8…

Doug Baba: www.doc88.com/p-201892780…

ZPL directive Preview:

labelary.com/viewer.html