C# pos 热敏打印 通用版(usb)

//定义一个字符串流,用来接收所要打印的数据

        private StringReader sr;
        public bool Print(string str)//str要打印的数据
        {
            bool result = true;
            try
            {
                sr = new StringReader(str.ToString());
                PrintDocument pd = new PrintDocument();
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();
                pd.DefaultPageSettings.Margins.Top = 2;
                pd.DefaultPageSettings.Margins.Left = 0;
                pd.DefaultPageSettings.PaperSize.Width = 320;
                pd.DefaultPageSettings.PaperSize.Height = 5150;
                pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机
                pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                pd.Print();

            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            return result;
        }
        public void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Font printFont = new Font("Arial", 7);//打印字体

            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            String line = "";

            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
            while (count < linesPerPage && ((line = sr.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

        public void PrintOrder(string order)
        {
            var r = JsonConvert.DeserializeObject<Result>(order);
            StringBuilder sb = new StringBuilder();
            int TotalPrice = 0;
            
            //int num = 32 - int.Parse(r.StoreName.toString().Length);
            //string str = "";
            //for (int i = 0; i < num; i++)
            //{
            //    str += " ";
            //}
            //sb.Append(""+str+""+ r.StoreName + "\n");

            sb.Append("                       " + r.StoreName + "\n");
            sb.Append("-----------------------------------------------------------------\n");
            sb.Append("收银员:" + r.OperatorName + "     " + "流水号:" + r.PurchaseNo + "\n");
            sb.Append("时间:" + DateTime.Now + "  " + " " + " " + "\n");
            sb.Append("-----------------------------------------------------------------\n");
            sb.Append("序号" + "  " + "商品名" + "  " + "数量" + "  " + "单价(元)" + "  " + "金额(元)" + "\n");

            foreach (var item in r.Details)
            {
                sb.Append(@"      " + r.Details.IndexOf(item) + "   " + "" + item.Title + "\n                        "
                        + item.Number + "        " + item.Price + "        " + item.Total + "                         " + "\n");
                TotalPrice += int.Parse(item.Total);
            }
            sb.Append("-----------------------------------------------------------------\n");
            //sb.Append("实收:" + (TotalPrice).ToString() + "                             \n");
            sb.Append("总价:" + r.OriginalTotal + "   实收:" + r.PayTotal + "                      \n");
            sb.Append("-----------------------------------------------------------------\n");
            sb.Append("             " + "谢谢光临,欢迎下次光顾!\n");
            //sb.Append("                   " + r.PurchaseNo);

            // updateData(int.Parse(jp.Result[0].Status));
            //}
            Print(sb.ToString());
        }
    }
    public class Info
    {
        public string Code { get; set; }
        public string Msg { get; set; }
        public List<Result> Result { get; set; }
        public string IsAmortize { get; set; }
        public string Point { get; set; }
        public string OriginalTotal { get; set; }
        public string PointTotal { get; set; }
    }
    public class Result
    {
        public string Id { get; set; }
        public string PurchaseNo { get; set; }
        public string CreateTime { get; set; }
        public string StoreId { get; set; }
        public string StoreName { get; set; }
        public string OperatorName { get; set; }
        public string Creator { get; set; }
        public string CustomerId { get; set; }
        public string Status { get; set; }
        public string IsPrint { get; set; }
        public string DiscountMoney { get; set; }
        public string Type { get; set; }
        public string Updateor { get; set; }
        public string UpdateTime { get; set; }
        public string Total { get; set; }
        public string PayTotal { get; set; }
        public string OriginalTotal { get; set; }
        
        public List<Details> Details { get; set; }

    }
    public class Details
    {
        public string Id { get; set; }
        public string ProductId { get; set; }
        public string Number { get; set; }
        public string Price { get; set; }
        public string Total { get; set; }
        public string WarehouseId { get; set; }
        public string AccountId { get; set; }
        public string PurchaseNo { get; set; }
        public string Price1 { get; set; }
        public string Encode { get; set; }
        public string Title { get; set; }
    }
    public class ProductInfo
    {
        public string Name { get; set; }
        public string Encode { get; set; }
        public int Quantity { get; set; }

        public decimal Price { get; set; }
    }

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值