C#實現(xiàn)的ZPL條碼打印類完整實例
本文實例講述了C#實現(xiàn)的ZPL條碼打印類。分享給大家供大家參考,具體如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; namespace Zebra { /// <summary> /// ZPL幫助類 /// </summary> public class ZebraHelper { /* * 打印中文先引用Fnthex32.dll dll控件常規(guī)安裝方法(僅供參考): 下面是32系統(tǒng)的注冊bat文件 可將下面的代碼保存為“注冊.bat“,放到dll目錄,就會自動完成dll注冊(win98不支持)。 @echo 開始注冊 copy Fnthex32.dll %windir%\system32\ regsvr32 %windir%\system32\Fnthex32.dll /s @echo Fnthex32.dll注冊成功 @pause 下面是64系統(tǒng)的注冊bat文件 @echo 開始注冊 copy Fnthex32.dll %windir%\SysWOW64\ regsvr32 %windir%\SysWOW64\Fnthex32.dll /s @echo Fnthex32.dll注冊成功 @pause * * ZebraHelper zh = new ZebraHelper(); StringBuilder builder = new StringBuilder(); 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()); string a = builder.ToString(); //打印 zh.CmdDos("c:\\c.txt", a); */ public string ZPL_Start() { StringBuilder builder = new StringBuilder(); builder.AppendLine("^XA"); //指令塊的開始 builder.AppendLine("^MD30"); //MD是設(shè)置色帶顏色的深度 return builder.ToString(); } public string ZPL_End() { StringBuilder builder = new StringBuilder(); builder.AppendLine("^XZ"); //指令塊的結(jié)束 return builder.ToString(); } /// <summary> /// 設(shè)置打印標簽紙邊距 /// </summary> /// <param name="printX">標簽紙邊距x坐標</param> /// <param name="printY">標簽紙邊距y坐標</param> /// <returns></returns> public string ZPL_PageSet(int printX, int printY) { StringBuilder builder = new StringBuilder(); builder.AppendLine("^LH" + printX + "," + printY); //定義條碼紙邊距 80 10 return builder.ToString(); } /// <summary> /// 打印憑條設(shè)置 /// </summary> /// <param name="width">憑條寬度</param> /// <param name="height">憑條高度</param> /// <returns>返回ZPL命令</returns> public string ZPL_SetLabel(int width, int height) { //ZPL條碼設(shè)置命令:^PW640^LL480 string sReturn = "^PW{0}^LL{1}"; return string.Format(sReturn, width, height); } /// <summary> /// 打印矩形 /// </summary> /// <param name="px">起點X坐標</param> /// <param name="py">起點Y坐標</param> /// <param name="thickness">邊框?qū)挾?lt;/param> /// <param name="width">矩形寬度,0表示打印一條豎線</param> /// <param name="height">矩形高度,0表示打印一條橫線</param> /// <returns>返回ZPL命令</returns> public string ZPL_DrawRectangle(int px, int py, int thickness, int width, int height) { //ZPL矩形命令:^FO50,50^GB300,200,2^FS string sReturn = "^FO{0},{1}^GB{3},{4},{2}^FS"; return string.Format(sReturn, px, py, thickness, width, height); } /// <summary> /// 打印英文 /// </summary> /// <param name="EnText">待打印文本</param> /// <param name="ZebraFont">打印機字體 A-Z</param> /// <param name="px">起點X坐標</param> /// <param name="py">起點Y坐標</param> /// <param name="Orient">旋轉(zhuǎn)角度N = normal,R = rotated 90 degrees (clockwise),I = inverted 180 degrees,B = read from bottom up, 270 degrees</param> /// <param name="Height">字體高度</param> /// <param name="Width">字體寬度</param> /// <returns>返回ZPL命令</returns> public string ZPL_DrawENText(string EnText,string ZebraFont, int px, int py, string Orient, int Height, int Width) { //ZPL打印英文命令:^FO50,50^A0N,32,25^FDZEBRA^FS string sReturn = "^FO{1},{2}^A" + ZebraFont + "{3},{4},{5}^FD{0}^FS"; return string.Format(sReturn, EnText, px, py, Orient, Height, Width); } /// <summary> /// 中文處理,返回ZPL命令 /// </summary> /// <param name="ChineseText">待轉(zhuǎn)變中文內(nèi)容</param> /// <param name="FontName">字體名稱</param> /// <param name="startX">X坐標</param> /// <param name="startY">Y坐標</param> /// <param name="Orient">旋轉(zhuǎn)角度0,90,180,270</param> /// <param name="Height">字體高度</param> /// <param name="Width">字體寬度,通常是0</param> /// <param name="IsBold">1 變粗,0 正常</param> /// <param name="IsItalic">1 斜體,0 正常</param> /// <returns></returns> public string ZPL_DrawCHText(string ChineseText, string FontName, int startX, int startY, int Orient, int Height, int Width, int IsBold, int IsItalic) { StringBuilder sResult = new StringBuilder(); StringBuilder hexbuf = new StringBuilder(21 * 1024); int count = ZebraHelper.GETFONTHEX(ChineseText, FontName, Orient, Height, Width, IsBold, IsItalic, hexbuf); if (count > 0) { string sEnd = "^FO" + startX.ToString() + "," + startY.ToString() + "^XGOUTSTR" + ",1,2^FS "; sResult.AppendLine(hexbuf.ToString().Replace("OUTSTR01", "OUTSTR") + sEnd); } return sResult.ToString(); } /// <summary> /// 打印條形碼(128碼) /// </summary> /// <param name="px">起點X坐標</param> /// <param name="py">起點Y坐標</param> /// <param name="width">基本單元寬度 1-10</param> /// <param name="ratio">寬窄比 2.0-3.0 增量0.1</param> /// <param name="barheight">條碼高度</param> /// <param name="barcode">條碼內(nèi)容</param> /// <returns>返回ZPL命令</returns> public string ZPL_DrawBarcode(int px, int py, int width, int ratio, int barheight, string barcode) { //ZPL打印英文命令:^FO50,260^BY1,2^BCN,100,Y,N^FDSMJH2000544610^FS string sReturn = "^FO{0},{1}^BY{2},{3}^BCN,{4},N,N^FD{5}^FS"; return string.Format(sReturn, px, py, width, ratio, barheight, barcode); } /// <summary> /// 中文處理 /// </summary> /// <param name="ChineseText">待轉(zhuǎn)變中文內(nèi)容</param> /// <param name="FontName">字體名稱</param> /// <param name="Orient">旋轉(zhuǎn)角度0,90,180,270</param> /// <param name="Height">字體高度</param> /// <param name="Width">字體寬度,通常是0</param> /// <param name="IsBold">1 變粗,0 正常</param> /// <param name="IsItalic">1 斜體,0 正常</param> /// <param name="ReturnPicData">返回的圖片字符</param> /// <returns></returns> [DllImport("fnthex32.dll")] public static extern int GETFONTHEX( string ChineseText, string FontName, int Orient, int Height, int Width, int IsBold, int IsItalic, StringBuilder ReturnPicData); /// <summary> /// 中文處理 /// </summary> /// <param name="ChineseText">待轉(zhuǎn)變中文內(nèi)容</param> /// <param name="FontName">字體名稱</param> /// <param name="FileName">返回的圖片字符重命</param> /// <param name="Orient">旋轉(zhuǎn)角度0,90,180,270</param> /// <param name="Height">字體高度</param> /// <param name="Width">字體寬度,通常是0</param> /// <param name="IsBold">1 變粗,0 正常</param> /// <param name="IsItalic">1 斜體,0 正常</param> /// <param name="ReturnPicData">返回的圖片字符</param> /// <returns></returns> [DllImport("fnthex32.dll")] public static extern int GETFONTHEX( string ChineseText, string FontName, string FileName, int Orient, int Height, int Width, int IsBold, int IsItalic, StringBuilder ReturnPicData); #region 擴展 /// <summary> /// 毫米轉(zhuǎn)像素 取整 /// </summary> /// <param name="mm">毫米</param> /// <param name="dpi">打印DPI 如300</param> /// <returns></returns> public double mm2px(double mm, double dpi) { double px = (mm / 25.4) * dpi; return Math.Round(px, 0, MidpointRounding.AwayFromZero); } /// <summary> /// 像素轉(zhuǎn)毫米 取整 /// </summary> /// <param name="px">像素</param> /// <param name="dpi">打印DPI 如300</param> /// <returns></returns> public double px2mm(double px, double dpi) { //像素轉(zhuǎn)換成毫米公式:(寬度像素/水平DPI)*25.4; double mm = (px / dpi) * 25.4; return Math.Round(mm, 0, MidpointRounding.AwayFromZero); } /// <summary> /// 生成zpl命令 且執(zhí)行 /// </summary> /// <param name="path">zpl文件路徑</param> /// <param name="zpl">zpl命令</param> public void CmdDos(string path,string zpl) { FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(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> /// <param name="command"></param> /// <returns></returns> private bool RunCmd(string command) { //實例一個Process類,啟動一個獨立進程 Process p = new Process(); //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性: p.StartInfo.FileName = "cmd.exe";//設(shè)定程序名 p.StartInfo.Arguments = "/c " + command;//設(shè)定程式執(zhí)行參數(shù) p.StartInfo.UseShellExecute = false;//關(guān)閉Shell的使用 p.StartInfo.RedirectStandardInput = true;//重定向標準輸入 p.StartInfo.RedirectStandardOutput = true;//重定向標準輸出 p.StartInfo.RedirectStandardError = true;//重定向錯誤輸出 p.StartInfo.CreateNoWindow = true;//設(shè)置不顯示窗口 //p.StandardInput.WriteLine(command);//也可以用這種方式輸入要執(zhí)行的命令 //p.StandardInput.WriteLine("exit");//不過要記得加上Exit要不然下一行程式執(zhí)行的時候會當機 try { p.Start();//開始進程 return true; } catch { } finally { if (p != null) p.Close(); } return false; } #endregion } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
支持windows與linux的php計劃任務(wù)的實現(xiàn)方法
這篇文章主要介紹了支持windows與linux的php計劃任務(wù)的實現(xiàn)方法,較為詳細的講述了php計劃任務(wù)中涉及到的php程序?qū)崿F(xiàn)方法、Windows計劃任務(wù)實現(xiàn)方法等,需要的朋友可以參考下2014-11-11