public bool Print2Bitmap(PrintLineModel m) { string fileName = "tmpBmp.bmp"; string text2 = Utils.Substitute(AppDomain.CurrentDomain.BaseDirectory, "\\") + fileName; try { FileTransferHttp.DownloadPicture(m.content, text2); } catch (Exception ex) { try { FileTransferHttp.GetImageByHttp(m.content, text2); } catch (Exception ex2) { LogTools.Debug(string.Format("[Pos 1003] 图片下载地址:{0}。下载异常两次:1:{1}。2:{2}。", m.content, ex.ToString(), ex2.ToString())); return(false); } } try { Invoke1.PrintMiddleBitmapFile0(new StringBuilder(text2), 0); } catch (Exception ex2) { LogTools.Debug(string.Format("[Pos 1003] 打印图片tmpBmp.bmp异常:{0}", ex2.ToString())); return(false); } return(true); }
private string PrintNewList(List <PrintLineModel> lines) { //Invoke1.PrintText0(new StringBuilder("1234567890一二三四五六七八九十abdefghijk一二三四五"), 32); if (lines == null || lines.Count == 0) { throw new Exception("[Pos 1000] 无打印数据!"); } try { LogTools.Info("开始打印"); for (int i = 0; i < lines.Count; i++) { PrintLineModel m = lines[i]; switch (m.type) { case 1: //图片 Print2Bitmap(m); break; case 2: //有安不支持条形码打印 break; case 3: Invoke1.PrintText0(new StringBuilder(m.content + Environment.NewLine), 32); //如果,有config_uan.txt文件。传几都不会有影响。 break; default: break; } } Invoke1.BeginPrint0(8); LogTools.Info("打印完毕,切纸"); return(""); } catch (Exception ex4) { throw new Exception("[Pos 1005] 打印失败," + ex4.Message); } finally { } }
private string PrintNewList(List <PrintLineModel> lines) { if (lines == null || lines.Count == 0) { throw new Exception("[Pos 1000] 无打印数据!"); } string appConfig = Utils.GetAppConfig("LptStr"); this.lc = new LptControl(appConfig); if (!this.lc.Open()) { throw new Exception("[Pos 1001] 设备驱动失败!"); } try { LogTools.Info("开始打印"); #region 循环打印 for (int i = 0; i < lines.Count; i++) { PrintLineModel m = lines[i]; ////LPT打印,不用手动隔行,因为它不会重叠 switch (m.type) { case 1: //图片和条形码都视作图片打印 case 2: this.lc.Write(new byte[] { 27, 64 }); this.lc.Write(new byte[] { 27, 33, 48 }); this.lc.Write(new byte[] { 27, 97, 1 }); //打印图片或条形码 try { this.Print2Bitmap(m.content, "tmpBmp.bmp", m.splitLine); } catch (Exception ex) { string sErr = string.Format("[Pos 1003] 打印图片{0},下载地址:{2}。\r\n异常:{1}" , "tmpBmp.bmp", ex.ToString(), m.content); //throw new Exception(); LogTools.Debug(sErr); } LptControl arg_164_0 = this.lc; byte[] array = new byte[3]; array[0] = 27; array[1] = 33; arg_164_0.Write(array); LptControl arg_186_0 = this.lc; byte[] array2 = new byte[3]; array2[0] = 27; array2[1] = 97; arg_186_0.Write(array2); //this.lc.Write(Environment.NewLine); break; case 3: StringBuilder sb = new StringBuilder(); switch (m.fontSize) { case 1: sb.Append(_1w1h); break; case 2: sb.Append(_1w2h); break; case 3: sb.Append(_2w2h); break; default: break; } sb.Append(m.content + Environment.NewLine); this.lc.Write(sb.ToString()); //LogTools.Info(string.Format("打印第{0}行,内容:{1}", i.ToString(), sb.ToString())); for (int j = 0; j < m.splitLine - 2; i++) { this.lc.Write(" " + Environment.NewLine); } break; default: break; } } #endregion #region 打印尾巴 this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(new byte[] { 27, 100, 3 }); this.lc.Write(new byte[] { 27, 109 }); LogTools.Info("打印完毕,切纸"); #endregion return(""); } catch (Exception ex4) { throw new Exception("[Pos 1005] 打印失败," + ex4.Message); } finally { this.lc.Close(); } }
private string PrintNewList(List <PrintLineModel> lines) { if (lines == null || lines.Count == 0) { throw new Exception("[Pos 1000] 无打印数据!"); } StringBuilder stringBuilder = new StringBuilder(""); string device_name = Utils.GetAppConfig("device_name"); int num = this.posPrinter.Open(device_name); if (num != 0) { throw new Exception("[Pos 1001] 设备驱动失败!返回码:" + num.ToString()); } try { LogTools.Info("开始打印"); #region 初始化 try { this.posPrinter.ClaimDevice(1000); this.posPrinter.DeviceEnabled = true; this.posPrinter.RecLineChars = 32; this.posPrinter.RecLetterQuality = true; this.posPrinter.TransactionPrint(2, 11); } catch (Exception ex2) { throw new Exception(string.Format("[Pos 1002] 打印初始化失败:{0}。", ex2.ToString())); } #endregion #region 循环打印 for (int i = 0; i < lines.Count; i++) { PrintLineModel m = lines[i]; #if DEBUG if (m.type == 1) { m.type = 2; m.content = "1234567890123"; } #endif #region 手动换行(而且要先打换行),如果不这么做,打印图像或高倍字体会重叠 if (i == 0) { //第一行就不用前置打换行了。顶头即可。 m.splitLine = 1; } for (int j = 0; j < m.splitLine - 1; j++) { this.posPrinter.PrintNormal(2, " " + Environment.NewLine); } #endregion switch (m.type) { case 1: #region 打印图片 LogTools.Info("打印图片开始"); try { this.Print2Bitmap(m.content, "tmpBmp.bmp", m.splitLine); } catch (Exception ex) { string sErr = string.Format("[Pos 1003] 打印图片{0},下载地址:{2}。\r\n异常:{1}" , "tmpBmp.bmp", ex.ToString(), m.content); LogTools.Debug(sErr); } LogTools.Info("打印图片结束"); #endregion break; case 2: #region 打印条码 LogTools.Info("打印条码开始"); string data = m.content; int codeWidth = 2; if (m.content.Length > 15) { codeWidth = 1; } num = this.posPrinter.PrintBarCode(2, data, 110, 60, codeWidth, -2, -13); if (num != 0) { LogTools.Info("[Pos 1003] 打印条码失败! 返回码:" + num.ToString()); throw new Exception("[Pos 1003] 打印条码失败! 返回码:" + num.ToString()); } LogTools.Info("打印条码完成"); #endregion break; case 3: #region 打文字 if (m.fontSize < 2) { m.splitLine = 1; } else { m.splitLine = 2; } StringBuilder sb = new StringBuilder(); switch (m.fontSize) { case 1: sb.Append(_1w1h); break; case 2: sb.Append(_1w2h); break; case 3: sb.Append(_2w2h); break; default: break; } sb.Append(m.content); sb.Append(Environment.NewLine); //LogTools.Info(string.Format("打印第{0}行,内容:{1}", i.ToString(), sb.ToString())); this.posPrinter.PrintNormal(2, sb.ToString()); #endregion break; default: break; } } #endregion #region 打印尾巴 this.posPrinter.PrintNormal(2, " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine); this.posPrinter.CutPaper(100); LogTools.Info("打印完毕,切纸"); #endregion return(""); } catch (Exception ex4) { throw new Exception("[Pos 1005] 打印失败," + ex4.ToString()); } finally { this.posPrinter.TransactionPrint(2, 12); this.posPrinter.RecLetterQuality = false; this.posPrinter.DeviceEnabled = false; this.posPrinter.ReleaseDevice(); this.posPrinter.Close(); } }