using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; using System.Drawing; //using System.Windows.Forms; namespace PrintSolution.LabelPrinter { public class ZebraPrinter : ZPL2Command { /******************* 标签位置 ***********************/ int blankOffset = 5; //边界宽带 单位:mm int labBoxLen1 = 26; //左边标签盒长度 单位:mm int labBoxLen2 = 29; //右边标签盒长度 单位:mm int fillBoxLen1 = 58; //左边填充盒长度 单位:mm int fillBoxLen2 = 60; //右边填充盒长度 单位:mm int rowHeight = 11; //行高 单位:mm int labHeadLen = 173; //标签头长度 单位:mm int labHeadWith = 29; //标签头宽带 单位:mm int offsetY = 0; //X坐标偏移量 单位:mm int offsetX = 0; //Y坐标偏移量 单位:mm int version = 2; //标签版本 1: 横向版本, 2: 纵向版本,3: 纵向版本(数据横着排),4: 横向版本,数据只有钢卷号、重量、生产日期 /******************* 可变参数 ***********************/ string strCmd = ""; // 指令 int xPos = 0; // X坐标 int yPos = 0; // Y坐标 int rowNum = 0; // 字段行数 /******************* 可变参数 ***********************/ /// /// 构造函数 /// public ZebraPrinter() : base() { initLabelSetting(); } /// /// 构造函数 /// public ZebraPrinter(string ipAddr) : base(ipAddr) { Addr = ipAddr; initLabelSetting(); } /// /// 标签版本属性 /// public int LableVersion { get { return version; } set { version = value; } } /// /// 变量初始化 /// public void initLabelSetting() { blankOffset = 5; //mm labBoxLen1 = 26; //mm labBoxLen2 = 29; //mm fillBoxLen1 = 58; // fillBoxLen2 = 60; // rowHeight = 11; labHeadLen = 173; labHeadWith = 29; offsetY = 0; offsetX = 0; // 指令初始化 strCmd = ""; // 指令 xPos = 0; // X坐标 yPos = 0; // Y坐标 rowNum = 0; // 字段行数 // FontDirect = 'B'; // 设定坐标偏移量 switch (version) { case 1: offsetX = 5; offsetY = 3; break; case 2: offsetX = 0; offsetY = -2; break; case 3: offsetX = 5; offsetY = 0; break; default: offsetX = 0; offsetY = -1; break; } } /// /// 把文字转换才Bitmap /// /// /// /// 用于输出的矩形,文字在这个矩形内显示,为空时自动计算 /// 字体颜色 /// 背景颜色 /// private Bitmap CreateImage(string text, Font font, Rectangle rect, Color fontcolor, Color backColor) { Graphics g; Bitmap bmp; StringFormat format = new StringFormat(StringFormatFlags.NoClip); if (rect == Rectangle.Empty) { bmp = new Bitmap(1, 1); g = Graphics.FromImage(bmp); //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图 SizeF sizef = g.MeasureString(text, font, PointF.Empty, format); int width = (int)(sizef.Width + 1); int height = (int)(sizef.Height + 1); rect = new Rectangle(0, 0, width, height); bmp.Dispose(); bmp = new Bitmap(width, height); } else { bmp = new Bitmap(rect.Width, rect.Height); } g = Graphics.FromImage(bmp); //使用ClearType字体功能 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; g.FillRectangle(new SolidBrush(backColor), rect); g.DrawString(text, font, Brushes.Black, rect, format); bmp.RotateFlip(RotateFlipType.Rotate270FlipNone); return bmp; } public static string Convert(Bitmap bms) { int b = 0; long n = 0; long clr; StringBuilder sb = new StringBuilder(); sb.Append("~DGR:ZONE.GRF,"); Bitmap bm = new Bitmap(bms); int w = ((bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)) * bm.Size.Height); int h = (bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)); sb.Append(w.ToString().PadLeft(5, '0') + "," + h.ToString().PadLeft(3, '0') + ",\n"); using (Bitmap bmp = new Bitmap(bm.Size.Width, bm.Size.Height)) { for (int y = 0; y < bm.Size.Height; y++) { for (int x = 0; x < bm.Size.Width; x++) { b = b * 2; clr = bm.GetPixel(x, y).ToArgb(); string s = clr.ToString("X"); if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0) { bmp.SetPixel(x, y, bm.GetPixel(x, y)); b++; } n++; if (x == (bm.Size.Width - 1)) { if (n < 8) { b = b * (2 ^ (8 - (int)n)); sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } if (n >= 8) { sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } } sb.Append("^LH0,0^FO354,600^XGR:ZONE.GRF^FS"); } return sb.ToString(); } public static string Convert2(Bitmap bms) { int b = 0; long n = 0; long clr; StringBuilder sb = new StringBuilder(); sb.Append("~DGR:ZONE1.GRF,"); Bitmap bm = new Bitmap(bms); int w = ((bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)) * bm.Size.Height); int h = (bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)); sb.Append(w.ToString().PadLeft(5, '0') + "," + h.ToString().PadLeft(3, '0') + ",\n"); using (Bitmap bmp = new Bitmap(bm.Size.Width, bm.Size.Height)) { for (int y = 0; y < bm.Size.Height; y++) { for (int x = 0; x < bm.Size.Width; x++) { b = b * 2; clr = bm.GetPixel(x, y).ToArgb(); string s = clr.ToString("X"); if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0) { bmp.SetPixel(x, y, bm.GetPixel(x, y)); b++; } n++; if (x == (bm.Size.Width - 1)) { if (n < 8) { b = b * (2 ^ (8 - (int)n)); sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } if (n >= 8) { sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } } // sb.Append("^LH0,0^FO1200,200^XGR:ZONE.GRF^FS"); } return sb.ToString(); } protected string ConvertImageToCode(Bitmap img) { var sb = new StringBuilder(); long clr = 0, n = 0; int b = 0; for (int i = 0; i < img.Size.Height; i++) { for (int j = 0; j < img.Size.Width; j++) { b = b * 2; clr = img.GetPixel(j, i).ToArgb(); string s = clr.ToString("X"); if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0) { b++; } n++; if (j == (img.Size.Width - 1)) { if (n < 8) { b = b * (2 ^ (8 - (int)n)); sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } if (n >= 8) { sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } sb.Append(System.Environment.NewLine); } return sb.ToString(); } /// /// 打印标签的一行数据 /// /// 左边格子中的数据 /// 右边格子中的数据 private void printLine(string leftData, string rightData) { switch (version) { case 1: // 打印左边的数据 xPos = (blankOffset + labBoxLen1 + offsetX) * getDotMM(); yPos = (blankOffset + labHeadWith + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, leftData); // 打印右边的数据, Y 坐标不变 xPos = (blankOffset + labBoxLen1 + fillBoxLen1 + labBoxLen2 + offsetX) * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); break; case 2: xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * getDotMM() + fontHeight; yPos = (blankOffset + offsetY) * getDotMM() + fillBoxLen2 * getDotMM() - getStringDots(rightData); //fillBoxLen2 strCmd += getFontCmd() + fillDataCmd(xPos, yPos, rightData); // X 坐标不变 //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * dotsMm +fontHeight ; yPos = (blankOffset + fillBoxLen2 + labBoxLen2 + offsetY) * getDotMM() + fillBoxLen1 * getDotMM() - getStringDots(leftData);// strCmd += getFontCmd() + fillDataCmd(xPos, yPos, leftData); break; case 3: /** 有标题的 // 打印左边的数据 strCmd += "^CFF,50,19\r\n"; //'^CFA,50,24'; xPos = (blankOffset + 2 + offsetX) * getDotMM(); yPos = (blankOffset + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, leftData); // 打印右边的数据, Y 坐标不变 strCmd += "^CFF,50,20\r\n"; //'^CFA,50,24'; xPos = (blankOffset + labBoxLen1 + 3) * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); **/ //只打印数据 strCmd += "^AFF,67,40\r\n"; //'^CFA,50,24'; xPos = (blankOffset + 10 + offsetX) * getDotMM(); yPos = (blankOffset + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); break; } rowNum++; } private void printstl(string leftData, string rightData) { switch (version) { case 1: // 打印左边的数据 xPos = (blankOffset + labBoxLen1 + offsetX) * getDotMM(); yPos = (blankOffset + labHeadWith + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, leftData); // 打印右边的数据, Y 坐标不变 xPos = (blankOffset + labBoxLen1 + fillBoxLen1 + labBoxLen2 + offsetX) * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); break; case 2: xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * getDotMM() + fontHeight; yPos = (blankOffset + offsetY) * getDotMM() + fillBoxLen2 * getDotMM() - getStringDots(rightData); //fillBoxLen2 strCmd += getFontCmd() + fillDataCmd(xPos, yPos, rightData); // X 坐标不变 //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * dotsMm +fontHeight ; yPos = (blankOffset + fillBoxLen2 + labBoxLen2 + offsetY) * getDotMM() + fillBoxLen1 * getDotMM() - getStringDots(leftData);// strCmd += getFontCmd() + fillDataCmd(xPos, yPos, leftData); break; case 3: strCmd += "^A0F,120,120\r\n"; //'^CFA,50,24'; xPos = (blankOffset + 10 + offsetX) * getDotMM(); yPos = (blankOffset + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); break; } rowNum++; } private void printstl2(string leftData, string rightData) { switch (version) { case 1: // 打印左边的数据 xPos = (blankOffset + labBoxLen1 + offsetX) * getDotMM(); yPos = (blankOffset + labHeadWith + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, leftData); // 打印右边的数据, Y 坐标不变 xPos = (blankOffset + labBoxLen1 + fillBoxLen1 + labBoxLen2 + offsetX) * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); break; case 2: xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * getDotMM() + fontHeight; yPos = (blankOffset + offsetY) * getDotMM() + fillBoxLen2 * getDotMM() - getStringDots(rightData); //fillBoxLen2 strCmd += getFontCmd() + fillDataCmd(xPos, yPos, rightData); // X 坐标不变 //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * dotsMm +fontHeight ; yPos = (blankOffset + fillBoxLen2 + labBoxLen2 + offsetY) * getDotMM() + fillBoxLen1 * getDotMM() - getStringDots(leftData);// strCmd += getFontCmd() + fillDataCmd(xPos, yPos, leftData); break; case 3: strCmd += "^A0F,200,200\r\n"; //'^CFA,50,24'; xPos = (blankOffset + 10 + offsetX) * getDotMM(); yPos = (blankOffset + offsetY) * getDotMM() + rowNum * rowHeight * getDotMM(); strCmd += fillDataCmd(xPos, yPos, rightData); break; } rowNum++; } /// /// 打印条码 /// /// 条码数据项 private void printBarcode(string data) { //int codeHeight = 150; int offset = 5; string orientation = "B"; // N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom) switch (version) { case 1: xPos = (offset + blankOffset )* getDotMM(); yPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetY) * getDotMM(); orientation = "N"; strCmd += getBarcodeCmd(xPos, yPos, orientation, data); break; case 2: xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offset) * getDotMM();//+ barcodeHeight yPos = labHeadLen * getDotMM() - data.Length * fontWidth; //(rowY + offsetY) * dotsMm + rowNum * rowHeight * dotsMm; orientation = "B"; strCmd += getBarcodeCmd(xPos, yPos, orientation, data); break; case 3: offset = 52; xPos = (offset + blankOffset) * getDotMM(); yPos = (blankOffset + 30 + rowNum * rowHeight + offsetY) * getDotMM(); orientation = "N"; strCmd += getBarcodeCmd(xPos, yPos, orientation, data); break; } } /// /// 打印标签 /// /// 标签内容 public void printLable(ColdCoilLable coilLabel) { initLabelSetting(); strCmd += getBeginCmd(); if(1 == version)strCmd += getDefaultFontCmd(); if (3 == version) //添加标签打印模板3 纸张竖着、数据横排 { blankOffset = 10;//为左边数据与边的间距; labBoxLen1 = 24; rowHeight = 10; //offsetY = 5; ////offsetX = 0; //strCmd += "^CFF,50,20\r\n"; //'^CFA,50,24'; //// 打印标签头名称 //xPos = (blankOffset + 2 + offsetX) * getDotMM(); //yPos = (blankOffset + 2 + offsetY) * getDotMM() + rowNum * 5 * getDotMM(); //strCmd += fillDataCmd(xPos, yPos, coilLabel.labelHeadName); //打印标签头,用labelHeadName来设置标签头名称,如:新钢冷轧厂冷硬卷标签 //rowNum++; offsetY = 10; printstl("COIL NO.:", coilLabel.coilNo); printLine("SPECIFICATION:", coilLabel.specification); printstl("GRADE:", coilLabel.steelGrade); printLine("HEAT NO.:", coilLabel.heatNo); printLine("DIMENSION(mm):", coilLabel.dimension); printLine("WEIGHT(kg):", coilLabel.weight); printLine("DATE:", coilLabel.prodDate); //if (null != coilLabel.checker && "" != coilLabel.checker) // printLine("CHECKER:", coilLabel.checker); printstl2("REMARKS:", coilLabel.steelGrade); //printBarcode(coilLabel.barcode); //取消条形码 strCmd += getEndCmd(); //writeDoc(strCmd); //sendMessage(Encoding.Default.GetBytes(strCmd)); sendCommand(strCmd); } else if (4 == version) //添加标签打印模板4 纸张竖着、数据竖着(成品包装作业管理) { /*offsetX = 5; strCmd += "^A0R,140,140\r\n"; //'^CFA,50,24';字体设置:第一个字母A表示粗体,第3个字母R表示字体纵向,F表示横向; // 打印标签钢卷号 //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * getDotMM() + fontHeight; //yPos = (blankOffset + offsetY) * getDotMM() + 15; //strCmd += fillDataCmd(xPos, yPos, coilLabel.coilNo); //钢卷号 xPos = (blankOffset + labHeadWith * 2 + rowNum * rowHeight + offsetX-48) * getDotMM() + fontHeight ; yPos = (blankOffset + offsetY + labHeadWith/2-10) * getDotMM() + 35; strCmd += fillDataCmd(xPos, yPos, coilLabel.coilNo); //钢卷号 rowNum++; offsetX = 0; strCmd += "^A0R,120,120\r\n";//"^A0R,75,60\r\n"; //'^CFA,50,24'; xPos = (blankOffset + labHeadWith + offsetX - 30) * getDotMM() + fontHeight;//(blankOffset + 23 + labHeadWith + offsetX-58+5) * getDotMM() + fontHeight; yPos = (blankOffset + offsetY + labHeadWith/2) * getDotMM() + 35; //fillBoxLen2 strCmd += fillDataCmd(xPos, yPos, coilLabel.weight + "kg"); // X 坐标不变 strCmd += "^A0R,120,120\r\n"; //'^CFF,50,20'; //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * dotsMm +fontHeight ; fillBoxLen1 * getDotMM() yPos = (blankOffset + 5 + offsetY + labHeadWith/2) * getDotMM() + fillBoxLen1 * getDotMM() - getStringDots(coilLabel.weight);// strCmd += fillDataCmd(xPos, yPos, coilLabel.prodDate.Replace("-","").Substring(0, 10)); //strCmd += fillDataCmd(xPos, yPos, coilLabel.prodDate); strCmd += getEndCmd(); //writeDoc(strCmd); //sendMessage(Encoding.Default.GetBytes(strCmd)); sendCommand(strCmd);*/ offsetX = 5; strCmd += "^A0R,140,140\r\n"; //'^CFA,50,24';字体设置:第一个字母A表示粗体,第3个字母R表示字体纵向,F表示横向; // 打印标签钢卷号 //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * getDotMM() + fontHeight; //yPos = (blankOffset + offsetY) * getDotMM() + 15; //strCmd += fillDataCmd(xPos, yPos, coilLabel.coilNo); //钢卷号 xPos = (blankOffset + labHeadWith * 2 + rowNum * rowHeight + offsetX - 40) * getDotMM() + fontHeight; yPos = (blankOffset + offsetY + labHeadWith / 2) * getDotMM() + 35; // yPos = (blankOffset + offsetY + labHeadWith/2-10) * getDotMM() + 35; strCmd += fillDataCmd(xPos, yPos, coilLabel.coilNo); //钢卷号 rowNum++; offsetX = 0; strCmd += "^A0R,120,120\r\n";//"^A0R,75,60\r\n"; //'^CFA,50,24'; xPos = (blankOffset + labHeadWith + offsetX - 20) * getDotMM() + fontHeight;//(blankOffset + 23 + labHeadWith + offsetX-58+5) * getDotMM() + fontHeight; yPos = (blankOffset + offsetY + labHeadWith / 2 + 10) * getDotMM() + 35; //fillBoxLen2 strCmd += fillDataCmd(xPos, yPos, coilLabel.weight + "kg"); // X 坐标不变 strCmd += "^A0R,120,120\r\n"; //'^CFF,50,20'; //xPos = (blankOffset + labHeadWith + rowNum * rowHeight + offsetX) * dotsMm +fontHeight ; fillBoxLen1 * getDotMM() yPos = (blankOffset + 5 + offsetY + labHeadWith / 2) * getDotMM() + fillBoxLen1 * getDotMM() - getStringDots(coilLabel.weight);// strCmd += fillDataCmd(xPos, yPos, coilLabel.prodDate.Replace("-", "").Substring(0, 10)); String BarCode = ""; BarCode += coilLabel.coilNo + ";"; //BarCode += "*XINSTEEL* CoilNo:" + coilLabel.coilNo + " "; //BarCode += "Dimension:" + coilLabel.dimension + " "; //BarCode += "SteelGrade:" + coilLabel.steelGrade + " "; //BarCode += "Date:" + coilLabel.prodDate + " "; BarCode += coilLabel.weight + "kg"; xPos = (blankOffset + labHeadWith + offsetX - 30) * getDotMM() + fontHeight; yPos = (blankOffset) * getDotMM(); strCmd += fillBQDataCmd(xPos, yPos, BarCode); //strCmd += fillDataCmd(xPos, yPos, coilLabel.prodDate); strCmd += getEndCmd(); //writeDoc(strCmd); //sendMessage(Encoding.Default.GetBytes(strCmd)); sendCommand(strCmd); } else if (5 == version) //添加标签打印模板4 纸张竖着、数据竖着(成品包装作业管理) { offsetX = 15; strCmd += "^A0B,80,60\r\n";// "^A0B,80,60\r\n"; //'^CFA,50,24';字体设置:第一个字母A表示粗体,第3个字母R表示字体纵向,F表示横向; // 打印标签钢卷号 xPos = (labHeadWith + rowNum * rowHeight + offsetX + rowHeight) * getDotMM(); yPos = (blankOffset + offsetY + fillBoxLen1) * getDotMM() - getStringDots(coilLabel.coilNo)-52;//yPos = (blankOffset + offsetY ) * getDotMM() + 15 strCmd += fillDataCmd(xPos, yPos, coilLabel.coilNo); //钢卷号 strCmd += "^A0B,75,60\r\n"; //'^CFA,50,24'; yPos = (blankOffset + offsetY + fillBoxLen2*2 + labBoxLen1) * getDotMM() + 15 - getStringDots(coilLabel.specification);//yPos = (blankOffset + offsetY + fillBoxLen2 + labBoxLen1) * getDotMM() + 15; //fillBoxLen2 strCmd += fillDataCmd(xPos, yPos, coilLabel.specification);//标准号 rowNum++; strCmd += "^A0B,80,60\r\n"; // 打印标签牌号炉号 xPos = (labHeadWith + rowNum * rowHeight + offsetX + rowHeight) * getDotMM(); yPos = (blankOffset + offsetY + fillBoxLen1) * getDotMM() - getStringDots(coilLabel.heatNo)-52;//yPos = (blankOffset + offsetY ) * getDotMM() + 15; strCmd += fillDataCmd(xPos, yPos, coilLabel.heatNo); //炉次号 strCmd += "^A0B,75,60\r\n"; //'^CFA,50,24'; yPos = (blankOffset + offsetY + fillBoxLen2 * 2 + labBoxLen1) * getDotMM() + 15 - getStringDots(coilLabel.steelGrade)-26;//yPos = (blankOffset + offsetY + fillBoxLen2 + labBoxLen1 ) * getDotMM() + 15; //fillBoxLen2 strCmd += fillDataCmd(xPos, yPos, coilLabel.steelGrade);//牌号 rowNum++; strCmd += "^A0B,80,60\r\n"; // 打印标签规格重量 xPos = (labHeadWith + rowNum * rowHeight + offsetX + rowHeight) * getDotMM(); yPos = (blankOffset + offsetY + fillBoxLen1) * getDotMM() - getStringDots(coilLabel.weight)-60;//yPos = (blankOffset + offsetY) * getDotMM() + 15; strCmd += fillDataCmd(xPos, yPos, coilLabel.weight); //重量 strCmd += "^A0B,75,60\r\n"; //'^CFA,50,24'; yPos = (blankOffset + offsetY + fillBoxLen2 * 2 + labBoxLen1) * getDotMM() + 15 - getStringDots(coilLabel.dimension);//yPos = (blankOffset + offsetY + fillBoxLen2 + labBoxLen1) * getDotMM() + 15; //fillBoxLen2 strCmd += fillDataCmd(xPos, yPos, coilLabel.dimension);//规格 rowNum++; strCmd += "^A0B,80,60\r\n"; // 打印标签日期合同号 xPos = (labHeadWith + rowNum * rowHeight + offsetX + rowHeight) * getDotMM(); yPos = (blankOffset + offsetY + fillBoxLen1) * getDotMM() - getStringDots(coilLabel.prodDate);//yPos = (blankOffset + offsetY) * getDotMM() + 15; strCmd += fillDataCmd(xPos, yPos, coilLabel.prodDate); //日期 strCmd += "^A0B,75,60\r\n"; //'^CFA,50,24'; yPos = (blankOffset + offsetY + fillBoxLen2 * 2 + labBoxLen1) * getDotMM() + 15 - getStringDots(coilLabel.contractNo)+13;//yPos = (blankOffset + offsetY + fillBoxLen2 + labBoxLen1) * getDotMM() + 15; //fillBoxLen2 strCmd += fillDataCmd(xPos, yPos, coilLabel.contractNo);//合同号 String BarCode = ""; BarCode += "CoilNo:"+coilLabel.coilNo + " "; BarCode += "Dimension:" + coilLabel.dimension + " "; BarCode += "SteelGrade:" + coilLabel.steelGrade + " "; BarCode += "Date:" + coilLabel.prodDate + " "; BarCode += "Weight:" + coilLabel.weight; xPos = (blankOffset + labHeadWith / 2+3) * getDotMM() + fontHeight; yPos = (blankOffset) * getDotMM(); strCmd += fillBQDataCmd(xPos, yPos, BarCode); //strCmd += "^FO100,100"; //strCmd += "^BQ,2,10"; //strCmd += "^FDLA," + BarCode + "^FS"; /* System.Drawing.Color co = Color.Red; System.Drawing.Font fo = new System.Drawing.Font("微软雅黑", 45, System.Drawing.FontStyle.Bold); Bitmap img = this.CreateImage(coilLabel.custName + "\r\n" + coilLabel.licenseMark+ "\r\n" + coilLabel.prodName, fo, new System.Drawing.Rectangle(), co, co); String aa = Convert(img);*/ System.Drawing.Color co = Color.Red; System.Drawing.Font fo = new System.Drawing.Font("微软雅黑", 35, System.Drawing.FontStyle.Bold); Bitmap img = this.CreateImage(coilLabel.custName + "\r\n\r\n" + coilLabel.prodName + "\r\n" + coilLabel.licenseMark, fo, new System.Drawing.Rectangle(), co, co); String aa = Convert2(img); aa = aa + "^LH0,0^FO340," + "400" + "^XGR:ZONE1.GRF^FS";//1145 string tempstr = coilLabel.checker; string tempstr1 = ""; int len = tempstr.Length; if (len > 40) { tempstr1 = tempstr.Substring(0, 40); tempstr = tempstr.Substring(40, len - 40); } Bitmap img2 = this.CreateImage(tempstr1 + "\r\n" + tempstr, fo, new System.Drawing.Rectangle(), co, co); // Bitmap img2 = this.CreateImage(tempstr, fo, new System.Drawing.Rectangle(), co, co); String aa2 = Convert2(img2); // aa2 = aa2 + "^LH0,0^FO1145,300^XGR:ZONE1.GRF^FS";//1145 aa2 = aa2 + "^LH0,0^FO1145,"+yPos+"^XGR:ZONE1.GRF^FS";//1145 var t = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); var w = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); string zpl = string.Format("^IDB:imgName.GRF~DGR:imgName.GRF,{0},{1},{2}", t, w, aa); //strCmd += fillDataCmd(0, 0, zpl + "^LH" + xPos.ToString() + ",600" + "^FO5,60^XGR:imgName.GRF");//牌号 strCmd += aa; strCmd += aa2; strCmd += getEndCmd(); sendCommand(strCmd); } else if (6 == version) //添加标签打印模板4 纸张竖着、数据竖着(成品包装作业管理) { xPos = (blankOffset + labHeadWith / 2 + 5) * getDotMM() + fontHeight; yPos = (blankOffset) * getDotMM(); System.Drawing.Color co = Color.Red; System.Drawing.Font fo = new System.Drawing.Font("", 20, System.Drawing.FontStyle.Bold); Bitmap img = this.CreateImage("中国", fo, Rectangle.Empty, co, co); String aa = Convert(img); var t = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); var w = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); //string zpl = string.Format("~DGR:imgName.GRF,{0},{1},{2}", t, w, aa); //strCmd += fillDataCmd(xPos, yPos, zpl + "^LH0,0^FO5,60^XGR:imgName.GRF");//牌号 //strCmd += "^XA~DGR:imgName.GRF,"; //strCmd += zpl; //strCmd += "^LH0,0^FO5,60^XGR:imgName.GRF^FS^XZ"; strCmd += aa; // strCmd += getEndCmd(); sendCommand(strCmd); } else { printLine(coilLabel.rollNo, coilLabel.specification); printLine(coilLabel.coilNo, coilLabel.steelGrade); printLine(coilLabel.heatNo, coilLabel.dimension); printLine(coilLabel.contractNo, coilLabel.weight); printLine(coilLabel.prodDate, coilLabel.checker); printLine(coilLabel.licenseNo, coilLabel.licenseMark); printBarcode(coilLabel.barcode); strCmd += getEndCmd(); sendCommand(strCmd); } } //测试写入数据到文本文件中 public void writeDoc(string str) { string filePath = "d:\\infor.txt";//这里是你的已知文件,需先创建此文件 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs); fs.SetLength(0);//首先把文件清空了。 sw.Write(str);//写你的字符串。 sw.Close(); } /// /// 测试函数 /// public void test() { ColdCoilLable coilLabel = new ColdCoilLable(); coilLabel.rollNo = "L1-017355-10"; coilLabel.specification = "GB2009-2011XGSPC"; coilLabel.steelGrade = "SPCC"; coilLabel.dimension = "1.8*1205"; coilLabel.coilNo = "L1-017355-10AA0"; coilLabel.weight = "28050"; coilLabel.heatNo = "J12-56790A"; coilLabel.prodDate = DateTime.Now.ToString("yyyyMMddhhmmss"); coilLabel.contractNo = "532011080001"; coilLabel.licenseNo = "E532011"; coilLabel.licenseMark = "XGHG"; coilLabel.checker = ""; coilLabel.barcode = coilLabel.coilNo; printLable(coilLabel); } } }