C#打印小票自带条形码打印

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chinaboykai/article/details/51104493
 private void btnReceipts_Click(object sender, EventArgs e)
        {
            string sheet_no = this.dgvOrders.SelectedRows[0].Cells[0].Value.ToString(); 
            jzPrint(sheet_no);

        }


        private void jzPrint(string sheet_no)
        {
            //第三部,进行打印
            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = docToPrint;
            this.docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage);
            // 调用PrintDialog的ShowDialog函数显示打印对话框 
            PrintDocument printDocument = new PrintDocument();
            printDocument.PrintPage += new PrintPageEventHandler(this.docToPrint_PrintPage);
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = printDocument;
            try
            {
                printPreviewDialog.ShowDialog();
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //DialogResult result = PrintDialog1.ShowDialog();
            //if (result == DialogResult.OK)// 弹出设置打印机,如果不需要设置,第三部可简写为   docToPrint.Print(); 则开始进行打印了
            //{
            //    // 开始打印 
            //    docToPrint.Print();

            //} 
        } 
        private void docToPrint_PrintPage(object sender, PrintPageEventArgs e)
        {
            string sheet_no = this.dgvOrders.SelectedRows[0].Cells[0].Value.ToString();// "WX-2016323163452";
           
            string text = null;
            // 信息头 
            string strTou = string.Empty;
            System.Drawing.Font printFont = new System.Drawing.Font
            ("Arial", 8, System.Drawing.FontStyle.Regular);
            System.Drawing.Font printFont1 = new System.Drawing.Font
            ("Arial", 11, System.Drawing.FontStyle.Regular);
            text = GetTicketString(sheet_no);
            string body = text;
            // 获取信息头 
            strTou = text.Substring(0, 40);
            //信息其他部分 
            text = text.Substring(40, (text.Length - 40));
            // 设置信息打印格式 
            e.Graphics.DrawString(strTou, printFont1, System.Drawing.Brushes.Black, 5, 5);
            e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 10, 5);
            //获取当前贴图的Height
            float now_x = e.Graphics.MeasureString(body, printFont).Height;
            //条形码打印
            BarCode.Code128 c = new BarCode.Code128();
            Bitmap bit = c.GetCodeImage(sheet_no, BarCode.Code128.Encode.Code128A);
            e.Graphics.DrawImage(bit, 10, 5 + now_x, 220, 50);
            //客户签字生成
            StringBuilder foot = new StringBuilder();
            foot.Append("-----------------------------------------------------\n");
            foot.Append("客户签名:\n");
            foot.Append("                                                \n");
            foot.Append("                                                \n");
            foot.Append("                                                \n");
            foot.Append("-----------------------------------------------------");
            e.Graphics.DrawString(foot.ToString(), printFont, System.Drawing.Brushes.Black, 10, 60 + now_x);


        }
        private string GetTicketString(string sheet_no)
        {
            string sql_order = string.Format(@"select a.sheet_no,a.orderman,a.ordertel,a.oper_date,
sum(a.real_qty) as real_qty,sum((a.price*a.real_qty)) as amt,
sum(a.Fact_real_qty) as Fact_real_qty,sum(a.Fact_Total) as fact_amt,
(sum( a.price*a.real_qty)  - sum(a.Fact_Total)) as diff,
(case when isnull(a.status,9) = 0 then '未发货' when a.status = 1 then '已发货' when a.status = 2 then '已取消' when a.status = 3 then '发货中' else '' end) as status,(case isnull(a.pay_type,0) when  1 then '储值卡支付'  when 2 then '微信支付' else '线下支付' end) as pay_type,
(case when isnull(a.deal_type,1) = 1 then '送货上门' else '门店自提' end) as deal_type,
(case when isnull(a.paystatus,0) = 1 then '已支付' else '未支付' end) as paystatus,ISNULL(send_address,address) as send_address,memo 
FROM t_order_bill_weixin a left join t_sys_operator o on o.oper_id=a.modify_oper
where    a.sheet_no='{0}'
group by a.sheet_no,a.orderman,a.ordertel,a.oper_date,a.branch_no,a.status,pay_type,deal_type,send_address,memo,
address,paystatus,o.oper_name,a.dealtime
", sheet_no);
            DataTable dt = Query.ProcessSql(sql_order, Common.PublicDAL.DbName);

            //收银打印  
            //string path = Application.StartupPath.ToString() + "\\ticket.txt";//exe程序所在的路径 
            StringBuilder sw = new StringBuilder();
            sw.Append("               苏州万家生鲜        \n");
            sw.Append("                                                \n");
            sw.Append("                                                \n");
            sw.Append("订单编号:" + dt.Rows[0]["sheet_no"].ToString() + "\n");
            sw.Append("支付方式:" + dt.Rows[0]["pay_type"].ToString() + "\n");
            sw.Append("成交时间:" + dt.Rows[0]["oper_date"].ToString() + "\n");
            sw.Append("客户姓名:" + dt.Rows[0]["orderman"].ToString() + "\n");
            sw.Append("客户电话:" + dt.Rows[0]["ordertel"].ToString() + "\n");
            sw.Append("客户地址:" + dt.Rows[0]["send_address"].ToString() + "\n");
            sw.Append("订单备注:" + dt.Rows[0]["memo"].ToString() + "\n");
            sw.Append("-----------------------------------------------------" + "\n");
            string sql_order_details = string.Format(@"select item_name, real_qty,price,(real_qty*price) as total   from t_order_bill_weixin where sheet_no='{0}' ", sheet_no);
            DataTable dt_details = Query.ProcessSql(sql_order_details, Common.PublicDAL.DbName);
            //sw.Append("物品               数量                  单价(¥)");
            sw.Append("品名               单价       数量       金额       " + "\n");
            sw.Append("-----------------------------------------------------" + "\n");
            int nums = 20;
            int prices = 12;
            decimal total = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string name = dt_details.Rows[i]["item_name"].ToString();//获取该行的物品名称
                string num = dt_details.Rows[i]["real_qty"].ToString();//数量
                string price = dt_details.Rows[i]["price"].ToString();//单价
                total += Convert.ToDecimal(num) * Convert.ToDecimal(price);
                int numlength = System.Text.Encoding.Default.GetBytes(num).Length;
                if (numlength < nums)
                {
                    num = AddSpace(num, nums - numlength);
                }

                int pricelength = System.Text.Encoding.Default.GetBytes(price).Length;
                if (pricelength < prices)
                {
                    price = AddSpace(price, prices - pricelength);
                }
                sw.Append(name + "\n");
                sw.Append("                      " + Convert.ToDecimal(price).ToString("#0.00") + "       " + Convert.ToDecimal(num).ToString("#0.00") + "       " + (Convert.ToDecimal(price) * Convert.ToDecimal(num)).ToString("#0.00") + "       " + "\n");

            }

            sw.Append("-----------------------------------------------------" + "\n");
            sw.Append("                  总数:" + dt.Rows.Count + "\n");
            //计算合计金额:  
            sw.Append("                  合计:" + total.ToString("#0.00") + "\n");//合计金额
            sw.Append("                  现金:" + total.ToString("#0.00") + "\n");//实收现金
            sw.Append("                  找赎:" + 0 + "\n");//实收现金
            sw.Append("                                         \t\t" + "\n");
            sw.Append("---------------------配送留存---------------------------" + "\n");
            sw.Append("运单号:" + dt.Rows[0]["sheet_no"].ToString() + "\n");
            return sw.ToString();
        }

        #region 该函数动态添加空格,对齐小票
        public string AddSpace(string text, int length)
        {
            text = text.PadRight(length, ' ');
            return text;
        }
        #endregion

展开阅读全文

没有更多推荐了,返回首页