标签机打印程序研究(二)
来源:互联网 发布:banner加载网络图片 编辑:程序博客网 时间:2024/05/20 00:12
public class PackLabel { public int Id { get; set; } public string Num { get; set; } public string PartNo { get; set; } public string BatchNo { get; set; } public List<newSheetLabel> SheetLableList { get; set; } public Queue<int> QueueInnerLabelCount; public MaterialInfo M { get; set; } public DateTime CreateDate { get; set; } public DateTime DOM { get; set; } public DateTime DOE { get; set; } public string WorkNum { get; set; } public int RealQuantity { get; set; } public string TBflag { get; set; } public string WorkOrder { get; set; } public string ContainRejectsState { get; set; } public string UnPackFlag { get; set; } public List<string> strlist { get; set; }//存放一些特殊字符,如“尾箱” public string PO { get; set; } public NewPackLabel() { //TBflag = StateHelper.FullBox; //ContainRejectsState = StateHelper.ExclusiveReject; //UnPackFlag = StateHelper.ProductPack; QueueInnerLabelCount=new Queue<int>(); strlist=new List<string> {"尾箱"}; } }
public class LabelPaper { public int Id { get; set; } public string LabelCode { get; set; } public string LabelDesc { get; set; } public string LabelPosition { get; set; } public int Width { get; set; } public int Height { get; set; } public string PaperDirection { get; set; } public bool UseState { get; set; } public DateTime AddTime { get; set; } public DateTime LastUpdateTime { get; set; } public string WorkNum { get; set; } public string ModelName { get; set; } public string ModelType { get; set; } public string FilePath { get; set; } public string OldLabelTypeInterface { get; set; } public int ModelCopies { get; set; } public byte[] PhotoName { get; set; } public string Remarks { get; set; } }
public class LabelPrint { //数据源:标签上的可变数据 private object _printData; public object DataSource { get { return _printData; } set { //if (value == null) //{ // throw new ArgumentNullException("value"); //} _printData = value; } } public List<LabelPaper> Papers { get; set; } //模板列表,数据与模板一对多,所以用List private string _printer; private PrintDocument printDoc; private string _generatedZpl;//替换标识符后新生成的zpl private int _labeWidth; private int _labelHeight; private string _paperDirection; private string _modelName; /// <summary> /// 当模板对象列表中包含了ZPL打印方式时,请确保传入的是Zebra打印设备 /// </summary> /// <param name="printer">打印机</param> public LabelPrint(string printer) { _printer = printer; printDoc = new PrintDocument(); printDoc.BeginPrint += printDoc_BeginPrint; printDoc.PrintPage += printDoc_PrintPage; } /// <summary> /// ZPL方式完成标签打印 /// </summary> /// <param name="paper">模板对象</param> /// <exception cref="NullReferenceException"></exception> /// <exception cref="TargetInvocationException"></exception> /// <exception cref="ArgumentException"></exception> private void PrintLabelByZPL(LabelPaper paper) { Type t = typeof(PrintHelper); Object[] parameters = new Object[2]; parameters[0] = paper.FilePath; parameters[1] = _printData; if (paper.ModelCopies < 5) { for (int i = 0; i < paper.ModelCopies; i++) { try { MethodInfo method = t.GetMethod(paper.ModelName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public); _generatedZpl = method.Invoke(null, parameters).ToString(); } catch (NullReferenceException ex) { Tools.WriteLog(ex); throw; } catch (TargetInvocationException ex) { Tools.WriteLog(ex); throw; } catch (ArgumentException ex) { Tools.WriteLog(ex); throw; } if (string.IsNullOrEmpty(_generatedZpl)) throw new NullReferenceException("GeneratedZpl属性为空,检查模板路径!"); //[0]是个占位符,代表zpl字符串中的打印数量 //只剩下数量字段需要替换,其它替换调用委托已完成 if (_generatedZpl.Contains("{C}")) _generatedZpl = _generatedZpl.Replace("{C}", "1"); RawPrinterHelper.SendStringToPrinter(_printer, _generatedZpl); } } else { try { MethodInfo method = t.GetMethod(paper.ModelName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public); _generatedZpl = method.Invoke(null, parameters).ToString(); } catch (NullReferenceException ex) { Tools.WriteLog(ex); throw; } catch (TargetInvocationException ex) { Tools.WriteLog(ex); throw; } catch (ArgumentException ex) { Tools.WriteLog(ex); throw; } if (string.IsNullOrEmpty(_generatedZpl)) throw new NullReferenceException("GeneratedZpl属性为空,检查模板路径!"); //[0]是个占位符,代表zpl字符串中的打印数量 //只剩下数量字段需要替换,其它替换调用委托已完成 if (_generatedZpl.Contains("{C}")) _generatedZpl = _generatedZpl.Replace("{C}", paper.ModelCopies.ToString(CultureInfo.InvariantCulture)); RawPrinterHelper.SendStringToPrinter(_printer, _generatedZpl); } } /// <summary> /// GDI方式打印初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printDoc_BeginPrint(object sender, PrintEventArgs e) { printDoc.PrintController = new StandardPrintController(); printDoc.PrinterSettings.PrinterName = _printer; printDoc.DefaultPageSettings.PaperSize = new PaperSize("Custom", PrintHelper.Mm2InchCenti(_labeWidth), PrintHelper.Mm2InchCenti(_labelHeight)); printDoc.DefaultPageSettings.Landscape = _paperDirection == StateHelper.horizontal; } /// <summary> /// GDI方式完成打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <exception cref="NullReferenceException"></exception> /// <exception cref="TargetInvocationException"></exception> /// <exception cref="ArgumentException"></exception> private void printDoc_PrintPage(object sender, PrintPageEventArgs e) { Type t = typeof(PrintHelper); Object[] parameters = new Object[2]; parameters[0] = e; parameters[1] = _printData; try { MethodInfo method = t.GetMethod(_modelName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public); method.Invoke(null, parameters); } catch (NullReferenceException ex) { Tools.WriteLog(ex); throw; } catch (TargetInvocationException ex) { Tools.WriteLog(ex); throw; } catch (ArgumentException ex) { Tools.WriteLog(ex); throw; } } /// <summary> /// 调用PrintDocument的Print方法 /// </summary> private void PrintLabelByGDI(LabelPaper paper) { _labeWidth = paper.Width; _labelHeight = paper.Height; _paperDirection = paper.PaperDirection; _modelName = paper.ModelName; for (int i = 0; i < paper.ModelCopies; i++) { printDoc.Print(); } } /// <summary> /// 完成ZPL和GDI两种标签打印方式,外部统一调用这个方法 /// </summary> public void PrintLabel() { foreach (LabelPaper paper in Papers) { if (!paper.UseState) continue; switch (paper.ModelType) { case StateHelper.ModelZPL: PrintLabelByZPL(paper); break; case StateHelper.ModelGDI: PrintLabelByGDI(paper); break; } } } /// <summary> /// 清理 /// </summary> public void DisposeLabelInfo() { printDoc = null; } }
class PrintHelper { /// <summary> /// 尾箱字样 /// </summary> /// <param name="e"></param> /// <param name="pack">pack中的strList包含了常量</param> public static void DrawLabelTailBox(PrintPageEventArgs e, NewPackLabel pack) { if (pack == null || pack.M == null) throw new ArgumentNullException("pack", "pack/M is null"); if (pack.strlist.Count > 0) e.Graphics.DrawString(pack.strlist[0], new Font("宋体", 130, FontStyle.Bold), Brushes.Black, 5, 30); } /// <summary> /// ZPL替换 /// </summary> /// <param name="filename"></param> /// <param name="pack"></param> /// <returns>ZPL字符串</returns> public static string P_Jar60mm100mm(string filename, NewPackLabel pack) { if (pack == null || pack.M == null) throw new ArgumentNullException("pack", "pack/M is null"); string data = "pack|" + pack.Num + "|" + pack.PartNo + "|" + pack.BatchNo + "|" + pack.RealQuantity + "|" + pack.DOM.ToString("yyyy/MM/dd"); string path = Path.Combine(Application.StartupPath, "ZPL", filename); if (File.Exists(path)) { string strZpl = File.ReadAllText(path); string strZpl1 = strZpl.Replace("{0}", pack.PartNo); string strZpl2 = strZpl1.Replace("{1}", pack.M.Description); string strZpl3 = strZpl2.Replace("{2}", pack.BatchNo); string strZpl4 = strZpl3.Replace("{3}", pack.M.Weight + "x" + pack.RealQuantity); string strZpl5 = strZpl4.Replace("{4}", pack.DOM.ToShortDateString()); string strZpl6 = strZpl5.Replace("{5}", pack.DOE.ToShortDateString()); string strZpl7 = strZpl6.Replace("{6}", pack.WorkNum); string strZplEnd = strZpl7.Replace("{7}", data); return strZplEnd; } return string.Empty; } }
说明:
1.PackLabel类:数据源
2.LabelPaper类,模板类
3.LabelPrint类,打印类
4.PrintHelper类,打印方法实现类,GDI和ZPL
0 0
- 标签机打印程序研究(二)
- 标签机打印程序研究
- 【干货#007】标签机打印小程序参数二维码的方法
- 标签机二次开发
- 程序中如何打印日志?(二)
- 标签机的使用
- ogre研究之第一个程序(二)
- 程序研究-
- 程序打印
- 程序打印
- VC++对话框程序打印及打印预览的实现(二)
- 研究java实现打印菱形
- ZPL II (Zebra 标签机)简易指令集
- C++ 实现多语言程序开发研究与Xtreme ToolkitPro的使用(二)
- 打印一首唐诗的N种实现之二:程序退出点修改
- petshop4.0研究(二)
- NHibernate 系列研究[二]
- Google 地图 研究 二
- Java核心技术点之多线程
- Spark(四): Spark-sql 读hbase
- Nginx基本功能极速入门
- 阿里淘宝架构学习:聊一聊淘宝首页和它背后的一套
- 摇一摇震动或播放音频案例以及分析详情
- 标签机打印程序研究(二)
- Qt5.7 + VS2015 环境搭建
- struts2的国际化
- Linux下的sort排序命令详解
- 空闲时间学一个Linux命令(14)—— head 命令
- 【Android开发VR实战】二.播放360°全景视频
- 在有原来的数据的情况下,恢复数据库的数据
- 函数重载(overload)和函数重写(override)的基本规则
- Elasticsearch安装