標籤:des style blog http color io os ar for
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.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,1050,SKJALDHAHDLKJAH,125.4701000758,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(); } }按鈕事件
C#列印相關的知識點