• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#实现打印源码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

打印的原理是:生成mdi文件,系统碰到mdi的时候会自动以打印的方式处理。所以,不管用什么模板,什么方式;能在PrintPage事件处理中,生成一张要打印内容的图片就OK了!

C#实现打印源码如下:

#region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            //打印预览
            //PrintPreviewDialog ppd = new PrintPreviewDialog();
            PrintDocument pd = new PrintDocument();
            //设置边距
            Margins margin = new Margins(20, 20, 20, 20);
            pd.DefaultPageSettings.Margins = margin;
            ////纸张设置默认
            //PaperSize pageSize = new PaperSize("First custom size", 800, 600);
            //pd.DefaultPageSettings.PaperSize = pageSize;
            //打印事件设置
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            //ppd.Document = pd;
            //ppd.ShowDialog();
            try
            {
                pd.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
            }
        }
        //打印事件处理
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            string date = lblDate.Text; //当前日期
            string flowId = lblFlowId.Text; //流水号
            string payDate = PayDate.Year.ToString() + "年" + PayDate.Month.ToString() + "月"; //应收年月
            string adminId = lblAdminId.Text; //操作员编号
            string baseExpense = lblBaseExpense.Text; //应交基本费用
            string fine = lblFine.Text; //罚款数目
            string upExpense = lblUpExpense.Text; //上月上余
            string actualExpense = txtActualExpense.Text; //实际应交费用
            string chineseExpense = DecimalToChinese.ConvertSum(actualExpense); //实际应交费用的中文大写

   //读取图片模板
            Image temp = Image.FromFile(@"Receipts.jpg");
            GetResultIntoImage(ref temp, UserId, flowId, date, baseExpense, fine, upExpense, actualExpense, chineseExpense, payDate, adminId);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = temp.Width;
            int height = temp.Height;
            Rectangle destRect = new Rectangle(x, y, width, height);
            e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
        }

        /// <summary>
        /// 将收费结果填充到图片模板
        /// </summary>
        private void GetResultIntoImage(
            ref Image temp,
            string userId,
            string flowId,
            string currentDate,
            string baseExpense,
            string fine,
            string upExpense,
            string actualExpense,
            string chineseExpense,
            string payDate,
            string adminName)
        {
            //读取图片模板
            Graphics g = Graphics.FromImage(temp);

            Font f = new Font("宋体", 12);
            Brush b = new SolidBrush(Color.Black);
           
   //填充数据到图片模板(位置要在制作图片模板的时候度量好)
            g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
            g.DrawString(userId, f, b, 168, 105);
            g.DrawString(UserName, f, b, 166, 134);
            g.DrawString(flowId, f, b, 535, 105);
            g.DrawString(currentDate, f, b, 535, 134);
            g.DrawString(baseExpense, f, b, 219, 202);
            g.DrawString(fine, f, b, 372, 202);
            g.DrawString(upExpense, f, b, 486, 202);
            g.DrawString(actualExpense, f, b, 596, 202);
            g.DrawString(chineseExpense, f, b, 196, 238);
            g.DrawString(payDate, f, b, 176, 269);
            g.DrawString(adminName, f, b, 497, 298);

            g.Dispose();
        }
        #endregion


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#编程的语法积累(一)发布时间:2022-07-10
下一篇:
C#读取DLL文件获取所有类发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap