这篇文章主要讲解了“C#中的打印原理以及应用实例”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中的打印原理以及应用实例”吧!
C#打印原理其实就是生成MDI文件,那么什么是MDI文件呢?MDI是虚拟打印的文档,系统碰到MDI的时候会自动以打印的方式处理。所以,不管用什么模板,什么方式;能在PrintPage事件处理中,生成一张要打印内容的图片就OK了!
C#打印原理应用实例:
#region 打印 private void btnPrint_Click(object sender, EventArgs e) { //C#打印原理之打印预览 //PrintPreviewDialog ppd = new PrintPreviewDialog(); PrintDocument pd = new PrintDocument(); //C#打印原理之设置边距 Margins margin = new Margins(20, 20, 20, 20); pd.DefaultPageSettings.Margins = margin; ////C#打印原理之纸张设置默认 //PaperSize pageSize = new PaperSize("First custom size", 800, 600); //pd.DefaultPageSettings.PaperSize = pageSize; //C#打印原理之打印事件设置 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()); } } //C#打印原理之打印事件处理 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); //实际应交费用的中文大写 //C#打印原理之读取图片模板 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> /// 将收费结果填充到图片模板 ///C#打印原理 /// </summary> private void GetResultIntoImage( ref Image temp, string userId, string flowId, string currentDate, string baseExpense, string actualExpense, string chineseExpense, string payDate, string adminName) { //C#打印原理之读取图片模板 Graphics g = Graphics.FromImage(temp); Font f = new Font("宋体", 12); Brush b = new SolidBrush(Color.Black); //C#打印原理之填充数据到图片模板(位置要在制作图片模板的时候度量好) 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(); }
感谢各位的阅读,以上就是“C#中的打印原理以及应用实例”的内容了,经过本文的学习后,相信大家对C#中的打印原理以及应用实例这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。