C# 打印机

   private void printDocument_PrintPage(object sender, PrintPageEventArgs ev)
        {

            Font titleFont = new Font("宋体", 9, FontStyle.Bold);//标题字体           

            Font fntTxt = new Font("宋体", 9, FontStyle.Regular);//正文文字           

            Brush brush = new SolidBrush(Color.Black);//画刷           

            Pen pen = new Pen(Color.Black);           //线条颜色           

            Point po = new Point(10, 10);

            try
            {

                ev.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po);   //DrawString方式进行打印。        

            }

            catch (Exception ex)
            {

                MessageBox.Show(this, "打印出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        ///GetPrintSw方法用来构造打印文本,内部StringBuilder.AppendLine在Drawstring时单独占有一行。

        public StringBuilder GetPrintSW()
        {

            StringBuilder sb = new StringBuilder();

            string tou = "测试管理公司名称";

            string address = "河南洛阳";

            string saleID = "2010930233330";    //单号       

            string item = "项目";

            decimal price = 25.00M;

            int count = 5;

            decimal total = 0.00M;

            decimal fukuan = 500.00M;

            sb.AppendLine(" " + tou + " \n");

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "单号:" + saleID);

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("项目" + "      " + "数量" + "    " + "单价" + "    " + "小计");

            for (int i = 0; i < count; i++)
            {

                decimal xiaoji = (i + 1) * price;

                sb.AppendLine(item + (i + 1) + "      " + (i + 1) + "     " + price + "    " + xiaoji);

                total += xiaoji;

            }

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("数量:" + count + "  合计: " + total);

            sb.AppendLine("付款:" + fukuan);

            sb.AppendLine("现金找零:" + (fukuan - total));

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("地址:" + address + "");

            sb.AppendLine("电话:123456789 123456789");

            sb.AppendLine("谢谢惠顾欢迎下次光临 ");

            sb.AppendLine("-----------------------------------------");

            return sb;

        }

触发事件

    private void btnPrint_Click(object sender, EventArgs e)
        {
            pd.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); //打印页面需指定相应的PrintDocument_PrintPrintPage事件委托     

            pd.Print();

        }

posted @ 2014-03-03 17:41  live in finally  阅读(258)  评论(0编辑  收藏  举报