溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中的打印原理以及應用實例

發(fā)布時間:2021-08-06 09:27:22 來源:億速云 閱讀:107 作者:chen 欄目:編程語言

這篇文章主要講解了“C#中的打印原理以及應用實例”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C#中的打印原理以及應用實例”吧!

C#打印原理其實就是生成MDI文件,那么什么是MDI文件呢?MDI是虛擬打印的文檔,系統(tǒng)碰到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;   //罰款數(shù)目   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#打印原理之填充數(shù)據(jù)到圖片模板(位置要在制作圖片模板的時候度量好)   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#中的打印原理以及應用實例”的內容了,經(jīng)過本文的學習后,相信大家對C#中的打印原理以及應用實例這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI