本文实例讲述了C#实现的ZPL条码打印类。分享给大家供大家参考,具体如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.IO; usingSystem.Diagnostics; usingSystem.Drawing; usingSystem.Drawing.Drawing2D; usingSystem.Runtime.InteropServices; namespaceZebra { ///<summary> ///ZPL帮助类 ///</summary> publicclassZebraHelper { /* *打印中文先引用Fnthex32.dll dll控件常规安装方法(仅供参考): 下面是32系统的注册bat文件 可将下面的代码保存为“注册.bat“,放到dll目录,就会自动完成dll注册(win98不支持)。 @echo开始注册 copyFnthex32.dll%windir%system32 regsvr32%windir%system32Fnthex32.dll/s @echoFnthex32.dll注册成功 @pause 下面是64系统的注册bat文件 @echo开始注册 copyFnthex32.dll%windir%SysWOW64 regsvr32%windir%SysWOW64Fnthex32.dll/s @echoFnthex32.dll注册成功 @pause * * ZebraHelperzh=newZebraHelper(); StringBuilderbuilder=newStringBuilder(); builder.AppendLine(zh.ZPL_Start()); builder.AppendLine(zh.ZPL_PageSet(40,80)); builder.AppendLine(zh.ZPL_DrawCHText("上善若水厚德载物","宋体",40,40,0,32,0,1,0)); builder.AppendLine(zh.ZPL_DrawBarcode(40,150,3,2,40,"111112222233333")); builder.AppendLine(zh.ZPL_DrawENText("111112222233333","A",30,205,"N",30,50)); builder.AppendLine(zh.ZPL_DrawRectangle(20,20,2,700,700)); builder.AppendLine(zh.ZPL_End()); stringa=builder.ToString(); //打印 zh.CmdDos("c:\c.txt",a); */ publicstringZPL_Start() { StringBuilderbuilder=newStringBuilder(); builder.AppendLine("^XA");//指令块的开始 builder.AppendLine("^MD30");//MD是设置色带颜色的深度 returnbuilder.ToString(); } publicstringZPL_End() { StringBuilderbuilder=newStringBuilder(); builder.AppendLine("^XZ");//指令块的结束 returnbuilder.ToString(); } ///<summary> ///设置打印标签纸边距 ///</summary> ///<paramname="printX">标签纸边距x坐标</param> ///<paramname="printY">标签纸边距y坐标</param> ///<returns></returns> publicstringZPL_PageSet(intprintX,intprintY) { StringBuilderbuilder=newStringBuilder(); builder.AppendLine("^LH"+printX+","+printY);//定义条码纸边距8010 returnbuilder.ToString(); } ///<summary> ///打印凭条设置 ///</summary> ///<paramname="width">凭条宽度</param> ///<paramname="height">凭条高度</param> ///<returns>返回ZPL命令</returns> publicstringZPL_SetLabel(intwidth,intheight) { //ZPL条码设置命令:^PW640^LL480 stringsReturn="^PW{0}^LL{1}"; returnstring.Format(sReturn,width,height); } ///<summary> ///打印矩形 ///</summary> ///<paramname="px">起点X坐标</param> ///<paramname="py">起点Y坐标</param> ///<paramname="thickness">边框宽度</param> ///<paramname="width">矩形宽度,0表示打印一条竖线</param> ///<paramname="height">矩形高度,0表示打印一条横线</param> ///<returns>返回ZPL命令</returns> publicstringZPL_DrawRectangle(intpx,intpy,intthickness,intwidth,intheight) { //ZPL矩形命令:^FO50,50^GB300,200,2^FS stringsReturn="^FO{0},{1}^GB{3},{4},{2}^FS"; returnstring.Format(sReturn,px,py,thickness,width,height); } ///<summary> ///打印英文 ///</summary> ///<paramname="EnText">待打印文本</param> ///<paramname="ZebraFont">打印机字体A-Z</param> ///<paramname="px">起点X坐标</param> ///<paramname="py">起点Y坐标</param> ///<paramname="Orient">旋转角度N=normal,R=rotated90degrees(clockwise),I=inverted180degrees,B=readfrombottomup,270degrees</param> ///<paramname="Height">字体高度</param> ///<paramname="Width">字体宽度</param> ///<returns>返回ZPL命令</returns> publicstringZPL_DrawENText(stringEnText,stringZebraFont,intpx,intpy,stringOrient,intHeight,intWidth) { //ZPL打印英文命令:^FO50,50^A0N,32,25^FDZEBRA^FS stringsReturn="^FO{1},{2}^A"+ZebraFont+"{3},{4},{5}^FD{0}^FS"; returnstring.Format(sReturn,EnText,px,py,Orient,Height,Width); } ///<summary> ///中文处理,返回ZPL命令 ///</summary> ///<paramname="ChineseText">待转变中文内容</param> ///<paramname="FontName">字体名称</param> ///<paramname="startX">X坐标</param> ///<paramname="startY">Y坐标</param> ///<paramname="Orient">旋转角度0,90,180,270</param> ///<paramname="Height">字体高度</param> ///<paramname="Width">字体宽度,通常是0</param> ///<paramname="IsBold">1变粗,0正常</param> ///<paramname="IsItalic">1斜体,0正常</param> ///<returns></returns> publicstringZPL_DrawCHText(stringChineseText,stringFontName,intstartX,intstartY,intOrient,intHeight,intWidth,intIsBold,intIsItalic) { StringBuildersResult=newStringBuilder(); StringBuilderhexbuf=newStringBuilder(21*1024); intcount=ZebraHelper.GETFONTHEX(ChineseText,FontName,Orient,Height,Width,IsBold,IsItalic,hexbuf); if(count>0) { stringsEnd="^FO"+startX.ToString()+","+startY.ToString()+"^XGOUTSTR"+",1,2^FS"; sResult.AppendLine(hexbuf.ToString().Replace("OUTSTR01","OUTSTR")+sEnd); } returnsResult.ToString(); } ///<summary> ///打印条形码(128码) ///</summary> ///<paramname="px">起点X坐标</param> ///<paramname="py">起点Y坐标</param> ///<paramname="width">基本单元宽度1-10</param> ///<paramname="ratio">宽窄比2.0-3.0增量0.1</param> ///<paramname="barheight">条码高度</param> ///<paramname="barcode">条码内容</param> ///<returns>返回ZPL命令</returns> publicstringZPL_DrawBarcode(intpx,intpy,intwidth,intratio,intbarheight,stringbarcode) { //ZPL打印英文命令:^FO50,260^BY1,2^BCN,100,Y,N^FDSMJH2000544610^FS stringsReturn="^FO{0},{1}^BY{2},{3}^BCN,{4},N,N^FD{5}^FS"; returnstring.Format(sReturn,px,py,width,ratio,barheight,barcode); } ///<summary> ///中文处理 ///</summary> ///<paramname="ChineseText">待转变中文内容</param> ///<paramname="FontName">字体名称</param> ///<paramname="Orient">旋转角度0,90,180,270</param> ///<paramname="Height">字体高度</param> ///<paramname="Width">字体宽度,通常是0</param> ///<paramname="IsBold">1变粗,0正常</param> ///<paramname="IsItalic">1斜体,0正常</param> ///<paramname="ReturnPicData">返回的图片字符</param> ///<returns></returns> [DllImport("fnthex32.dll")] publicstaticexternintGETFONTHEX( stringChineseText, stringFontName, intOrient, intHeight, intWidth, intIsBold, intIsItalic, StringBuilderReturnPicData); ///<summary> ///中文处理 ///</summary> ///<paramname="ChineseText">待转变中文内容</param> ///<paramname="FontName">字体名称</param> ///<paramname="FileName">返回的图片字符重命</param> ///<paramname="Orient">旋转角度0,90,180,270</param> ///<paramname="Height">字体高度</param> ///<paramname="Width">字体宽度,通常是0</param> ///<paramname="IsBold">1变粗,0正常</param> ///<paramname="IsItalic">1斜体,0正常</param> ///<paramname="ReturnPicData">返回的图片字符</param> ///<returns></returns> [DllImport("fnthex32.dll")] publicstaticexternintGETFONTHEX( stringChineseText, stringFontName, stringFileName, intOrient, intHeight, intWidth, intIsBold, intIsItalic, StringBuilderReturnPicData); #region扩展 ///<summary> ///毫米转像素取整 ///</summary> ///<paramname="mm">毫米</param> ///<paramname="dpi">打印DPI如300</param> ///<returns></returns> publicdoublemm2px(doublemm,doubledpi) { doublepx=(mm/25.4)*dpi; returnMath.Round(px,0,MidpointRounding.AwayFromZero); } ///<summary> ///像素转毫米取整 ///</summary> ///<paramname="px">像素</param> ///<paramname="dpi">打印DPI如300</param> ///<returns></returns> publicdoublepx2mm(doublepx,doubledpi) { //像素转换成毫米公式:(宽度像素/水平DPI)*25.4; doublemm=(px/dpi)*25.4; returnMath.Round(mm,0,MidpointRounding.AwayFromZero); } ///<summary> ///生成zpl命令且执行 ///</summary> ///<paramname="path">zpl文件路径</param> ///<paramname="zpl">zpl命令</param> publicvoidCmdDos(stringpath,stringzpl) { FileStreamfs=newFileStream(path,FileMode.Create,FileAccess.Write); StreamWritersw=newStreamWriter(fs,Encoding.Default);//ANSI编码格式 if(File.Exists(path)) { sw.Write(zpl); sw.Flush(); sw.Close(); } fs.Close(); RunCmd("print/d:COM1"+path+""); } ///<summary> ///运行Dos命令 ///</summary> ///<paramname="command"></param> ///<returns></returns> privateboolRunCmd(stringcommand) { //实例一个Process类,启动一个独立进程 Processp=newProcess(); //Process类有一个StartInfo属性,這個是ProcessStartInfo类,包括了一些属性和方法,下面我們用到了他的几个属性: p.StartInfo.FileName="cmd.exe";//设定程序名 p.StartInfo.Arguments="/c"+command;//设定程式执行参数 p.StartInfo.UseShellExecute=false;//关闭Shell的使用 p.StartInfo.RedirectStandardInput=true;//重定向标准输入 p.StartInfo.RedirectStandardOutput=true;//重定向标准输出 p.StartInfo.RedirectStandardError=true;//重定向错误输出 p.StartInfo.CreateNoWindow=true;//设置不显示窗口 //p.StandardInput.WriteLine(command);//也可以用这种方式输入要执行的命令 //p.StandardInput.WriteLine("exit");//不过要记得加上Exit要不然下一行程式执行的時候会当机 try { p.Start();//开始进程 returntrue; } catch { } finally { if(p!=null) p.Close(); } returnfalse; } #endregion } }
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#程序设计之线程使用技巧上述就是C#学习教程:C#实现的ZPL条码打印类完整实例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/907416.html