标签机打印程序研究(二)

来源:互联网 发布: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
原创粉丝点击