/// <summary> /// 打印左右对齐的行 /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> private byte[] PrintLineLeftRight(string left, string right, int fontSize = 1) { var zhLeft = PrinterCmdUtils.CalcZhQuantity(left); // 左边文本的中文字符长度 var enLeft = left.Length - zhLeft; // 左边文本的其他字符长度 var zhRight = PrinterCmdUtils.CalcZhQuantity(right); // 右边文本的中文字符长度 var enRight = right.Length - zhRight; // 右边文本的其他字符长度 var len = FormatLen - ((zhLeft * 2 + enLeft + zhRight * 2 + enRight) * fontSize); // 缺少的字符长度 if (len > 0) { for (int i = 0; i < len / fontSize; i++) { left += " "; } } else { var times = 1; while (true) { if (FormatLen * times + len > 0) { break; } times++; } for (int i = 0; i < (FormatLen * times + len) / fontSize; i++) { left += " "; } } return(TextToByte(left + right)); }
private int maxRightLen = 10; // 打印商品时,商品数量与商品价格最多占10个字符位 /// <summary> /// 打印订单商品 /// </summary> /// <param name="product"></param> /// <returns></returns> private List <byte[]> ProductLine(OrderProduct product, int fontSize = 1) { var left = product.Name; if (!string.IsNullOrEmpty(product.Description)) { left += "(" + product.Description + ")"; } var middle = "*" + Convert.ToDouble(product.Quantity); var right = Convert.ToDouble(product.Price) + ""; var place = string.Empty; for (int i = 0; i < maxRightLen - middle.Length - right.Length; i++) { place += " "; } right = middle + place + right; var buffer = PrinterCmdUtils.PrintLineLeftRight(left, right, fontSize: fontSize); return(new List <byte[]> { buffer }); }
/// <summary> /// 前台打印 /// </summary> /// <param name="order"></param> /// <param name="socket"></param> private void ReceptionPrint(Order order, Socket socket) { var bufferArr = new List <byte[]>(); // 打印当日序号 bufferArr.Add(PrinterCmdUtils.AlignCenter()); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(4)); bufferArr.Add(TextToByte("#" + order.Identifier)); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(2)); bufferArr.Add(TextToByte("简单猫")); bufferArr.Add(PrinterCmdUtils.NextLine()); // 打印小票类别 bufferArr.Add(PrinterCmdUtils.AlignLeft()); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(1)); bufferArr.Add(TextToByte("前台小票")); bufferArr.Add(PrinterCmdUtils.NextLine()); // 分隔 bufferArr.Add(PrinterCmdUtils.SplitLine("-", Format)); bufferArr.Add(PrinterCmdUtils.NextLine()); // 备注 if (!string.IsNullOrEmpty(order.Remark)) { bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(2)); bufferArr.Add(TextToByte($"备注:{order.Remark}")); bufferArr.Add(PrinterCmdUtils.NextLine()); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(1)); bufferArr.Add(PrinterCmdUtils.NextLine()); } // 商户名称 bufferArr.Add(PrinterCmdUtils.AlignCenter()); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(2)); bufferArr.Add(TextToByte(ApplicationObject.App.Business.Name)); bufferArr.Add(PrinterCmdUtils.NextLine()); bufferArr.Add(PrinterCmdUtils.AlignLeft()); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(1)); bufferArr.Add(PrinterCmdUtils.NextLine()); // 下单时间 bufferArr.Add(TextToByte($"下单时间:{order.PayTime:yyyy-MM-dd HH:mm:ss}")); bufferArr.Add(PrinterCmdUtils.NextLine()); // 订单编号 bufferArr.Add(TextToByte($"订单编号:{order.OrderCode}")); bufferArr.Add(PrinterCmdUtils.NextLine()); // 商品分隔 bufferArr.Add(PrinterCmdUtils.SplitText("-", "购买商品", Format)); bufferArr.Add(PrinterCmdUtils.NextLine()); // 打印商品 bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(2)); foreach (var product in order.Products) { var buffer = ProductLine(product, 2); buffer.ForEach(a => { bufferArr.Add(a); bufferArr.Add(PrinterCmdUtils.NextLine()); }); } bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(1)); // 分隔 bufferArr.Add(PrinterCmdUtils.SplitText("-", "其他", Format)); bufferArr.Add(PrinterCmdUtils.NextLine()); // 包装费 if (order.PackagePrice.HasValue) { bufferArr.Add(PrintLineLeftRight("包装费", double.Parse(order.PackagePrice + "") + "")); bufferArr.Add(PrinterCmdUtils.NextLine()); } // 配送费 bufferArr.Add(PrintLineLeftRight("配送费", double.Parse(order.Freight + "") + "")); bufferArr.Add(PrinterCmdUtils.NextLine()); // 满减活动打印 if (order.SaleFullReduce != null) { bufferArr.Add(PrintLineLeftRight(order.SaleFullReduce.Name, "-¥" + double.Parse(order.SaleFullReduce.ReduceMoney + "") + "")); bufferArr.Add(PrinterCmdUtils.NextLine()); } // 优惠券打印 if (order.SaleCouponUser != null) { bufferArr.Add(PrintLineLeftRight(order.SaleCouponUser.Name, "-¥" + order.SaleCouponUser.Value + "")); bufferArr.Add(PrinterCmdUtils.NextLine()); } // 订单金额 bufferArr.Add(PrinterCmdUtils.AlignRight()); bufferArr.Add(TextToByte("实付:")); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(2)); bufferArr.Add(TextToByte(order.Price + "元")); bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(1)); bufferArr.Add(PrinterCmdUtils.NextLine()); bufferArr.Add(PrinterCmdUtils.AlignLeft()); // 分隔 bufferArr.Add(PrinterCmdUtils.SplitLine("*", Format)); bufferArr.Add(PrinterCmdUtils.NextLine()); // 地址 bufferArr.Add(PrinterCmdUtils.FontSizeSetBig(2)); bufferArr.Add(TextToByte(order.ReceiverAddress)); bufferArr.Add(PrinterCmdUtils.NextLine()); bufferArr.Add(TextToByte(order.Phone)); bufferArr.Add(PrinterCmdUtils.NextLine()); bufferArr.Add(TextToByte(order.ReceiverName)); bufferArr.Add(PrinterCmdUtils.NextLine()); // 切割 bufferArr.Add(PrinterCmdUtils.FeedPaperCutAll()); // 打印 bufferArr.ForEach(a => socket.Send(a)); }