GDI+可以用来创建和处理图像,还可以“绘制文本”。人们通常认为,文本时打印出来的,不是绘制出来的,但是在.NET中,显示和渲染文本的技术类似于显示图形的技术:必须创建Graphics对象,然后利用该对象的方法来定位和渲染文本字符串。所以,字体(Font)是打印的基础知识,有关字体的知识请参阅MSDN http://msdn.microsoft.com/zh-cn/library/system.drawing.font(v=vs.110).aspx 。
System.Drawing.Printing命名空间的PrintDocument类提供了控制打印进程的方法、成员属性和事件,因此,要建立输出到打印机的程序,就首先要建立PrintDocument对象。
PrintDocument pDocument = new PrintDocument();
然后调用PrintDocument.Print方法启动打印进程,此时触发PrintDocument类的BeginPrint和PrintPage事件,PrintPage事件处理方法包含生成输出的逻辑和语句,包括确定打印行数、输出DrawString语句、负责通知底层的打印控制器是否还有待打印的页面(设置HasMorePages属性)等。触发流程如下:
private void FrmMain_Load(object sender, EventArgs e) { sr = new StreamReader(@"数据的路径(.txt)"); //数据格式如下 /* 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 1000758,1050,SKJALDHAHDLKJAH,125.470 */ }
加载数据
1 StreamReader sr; 2 string[] prtLine; 3 Font rptFont; 4 Font hdrFont; 5 string title = "Title"; 6 float lineHeight;
类级别的变量
private void pDocument_BeginPrint(object sender, PrintEventArgs e) { rptFont = new Font("Arial", 10); hdrFont = new Font(rptFont, FontStyle.Bold); }
BeginPrint事件,一般用于初始化字体等变量
private void pDocument_EndPrint(object sender, PrintEventArgs e) { rptFont.Dispose(); hdrFont.Dispose(); sr.Close(); }
EndPrint事件,一般用于释放资源,关闭数据连接等
private void pDocument_PrintPage(object sender, PrintPageEventArgs e) { Graphics graphics = e.Graphics; int xPos = e.MarginBounds.Left; int lineCt = 0; lineHeight = hdrFont.GetHeight(graphics); int linesPerPage = (int)((e.MarginBounds.Bottom - e.MarginBounds.Top) / lineHeight - 2); float yPos = 2 * lineHeight + e.MarginBounds.Top; int hdrPos = e.MarginBounds.Top; PrintHdr(graphics, hdrPos, 200); string prod; char[] delim = { ‘,‘ }; while ((prod = sr.ReadLine()) != null) { prtLine = prod.Split(delim, 4); yPos += lineHeight; PrintDetail(graphics, yPos); if (lineCt == linesPerPage) { e.HasMorePages = true; lineCt = 0; break; } lineCt++; } }
PrintPages事件,包括主要的打印逻辑以及HasMorePages属性的设置
private void PrintHdr(Graphics g, int yPos, int xPos) { g.DrawString(title, hdrFont, Brushes.Black, xPos, yPos); float[] ts = { 80, 80, 200 }; StringFormat strFmt = new StringFormat(); strFmt.SetTabStops(0, ts); g.DrawString("Code\tVender\tDescription\tCost", hdrFont, Brushes.Black, xPos, yPos + 2 * lineHeight, strFmt); }
打印大标题和列标题
private void PrintDetail(Graphics g, float yPos) { int xPos = 200; StringFormat strFmt = new StringFormat(); strFmt.Trimming = StringTrimming.EllipsisCharacter; strFmt.FormatFlags = StringFormatFlags.NoWrap; RectangleF r = new RectangleF(xPos + 160, yPos, 200, lineHeight); string invenid = prtLine[0]; string vendor = prtLine[1]; string desc = prtLine[2]; Decimal price = decimal.Parse(prtLine[3]); g.DrawString(invenid, rptFont, Brushes.Black, xPos, yPos); g.DrawString(vendor, rptFont, Brushes.Black, xPos + 80, yPos); g.DrawString(desc, rptFont, Brushes.Black, r, strFmt); strFmt.Alignment = StringAlignment.Far; strFmt.Trimming = StringTrimming.None; g.DrawString(price.ToString("#,###.00"), rptFont, Brushes.Black, xPos + 400, yPos, strFmt); }
打印详细内容
private void btnPrint_Click(object sender, EventArgs e) { PrintDocument pDocument = new PrintDocument(); PrintDialog pDialog = new PrintDialog(); pDialog.Document = pDocument; pDialog.AllowSomePages = true; PrintPreviewDialog preDialog = new PrintPreviewDialog(); preDialog.Document = pDocument; pDocument.PrintPage += new PrintPageEventHandler(pDocument_PrintPage); pDocument.BeginPrint += new PrintEventHandler(pDocument_BeginPrint); pDocument.EndPrint += new PrintEventHandler(pDocument_EndPrint); if (pDialog.ShowDialog() == DialogResult.OK) { pDocument.Print(); } }
按钮事件
时间: 2024-11-02 23:12:50