日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實現(xiàn)C#打印文檔實例詳解

我們在實際開發(fā)中會遇到實現(xiàn)C#打印文檔的需求,那么如何設計一個編輯處理程序呢,它可以實現(xiàn)編輯和打印、打印預覽文檔。那么下面我們就詳細向你介紹C#打印文檔的具體的操作和實現(xiàn)。

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序設計、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了博州免費建站歡迎大家使用!

C#打印文檔操作方式:

C#打印文檔1.新建一個項目

項目中有兩個form(Form1,Form2)

C#打印文檔2.在Form1中添加菜單

mainMenu1,一個richTextBox1(定義為Public),一個打印文檔控件PrintDocument,名稱為MyPrintDC。一個狀態(tài)欄名稱為myStatus。

菜單項有:

文件(mnFile){新建(mnNew),打開(mnOpen),保存(mnSave),頁面設置(mnPageSetup),打印預覽(mnPrintView),打印(mnPint),退出(mnClose)}

編輯(mnEdit){復制(mnCopy),剪切(mnCut),粘貼(mnPaste),查找(mnSearch)}

關于(mnAbout)

C#打印文檔3.在Form2中添加一個標簽:

查找內(nèi)容,文本(txtSearch),命令按鈕(btnSearch) 查找一下個,命令按鈕(btnCancel)取消4.Form1中代碼:

C#打印文檔之加入引用:

 
 
 
  1. using System.IO; 

C#打印文檔之在控件定義階段中加入:

 
 
 
  1. private StringReader myReader;  
  2.  
  3. private Form2 f;  

C#打印文檔之Form1窗體的構造函數(shù)中:

 
 
 
  1. f=new Form2();  
  2.  
  3. f.Owner =this;  
  4.  
  5. f.Hide();  

C#打印文檔之Form1窗體中定義一個方法CheckSave ()

 
 
 
  1. private void CheckSave()  
  2.  
  3. {  
  4.  
  5. if (this.richTextBox1.Text!="")  
  6.  
  7. {  
  8.  
  9. if (MessageBox.Show("是否保存當前文件?","確認",  
  10. MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)  
  11.  
  12. {  
  13.  
  14. this.myStatus.Text ="保存文件";  
  15.  
  16. SaveFileDialog svfDialog=new SaveFileDialog();  
  17.  
  18. svfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";  
  19.  
  20. if (svfDialog.ShowDialog()==DialogResult.OK)  
  21.  
  22. {  this.richTextBox1.SaveFile(svfDialog.FileName,  
  23. RichTextBoxStreamType.PlainText);  
  24.  
  25. }  
  26.  
  27. }  
  28.  
  29. }  
  30.  
  31. }  

C#打印文檔之新建菜單(mnNew):

 
 
 
  1. this.CheckSave();  
  2.  
  3. this.richTextBox1.Clear();  
  4.  
  5. this.myStatus.Text ="新建文件";  

C#打印文檔之打開菜單(mnOpen):

 
 
 
  1. this.CheckSave();  
  2.  
  3. OpenFileDialog opfDialog=new OpenFileDialog ();  
  4.  
  5. opfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";  
  6.  
  7. if (opfDialog.ShowDialog()==DialogResult.OK)  
  8.  
  9. {  this.richTextBox1.LoadFile(  
  10. opfDialog.FileName,RichTextBoxStreamType.PlainText);  
  11.  
  12. }  
  13.  
  14. this.myStatus.Text ="打開文件";  

C#打印文檔之保存菜單(mnSave):

 
 
 
  1. this.myStatus.Text ="保存文件";  
  2.  
  3. SaveFileDialog svfDialog=new SaveFileDialog();  
  4.  
  5. svfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";  
  6.  
  7. if (svfDialog.ShowDialog()==DialogResult.OK)  
  8.  
  9. {  
  10.  
  11. this.richTextBox1.SaveFile(svfDialog.FileName,  
  12. RichTextBoxStreamType.PlainText);  
  13.  
  14. }  

C#打印文檔之控件的PrintPage事件代碼(MyPrintDC):

 
 
 
  1. private void MyPrintDC_PrintPage(object sender,  
  2.  System.Drawing.Printing.PrintPageEventArgs e)  
  3.  
  4. {  
  5.  
  6. //打印文檔打印頁面事件代碼  
  7.  
  8. this.myReader=new StringReader(this.richTextBox1.Text);//定義字符讀流  
  9.  
  10. Graphics myGraphics=e.Graphics;  
  11.  
  12. Font myPrintFont=this.richTextBox1.Font;  
  13.  
  14. //計算一頁行數(shù)  
  15.  
  16. float iLinePage=  
  17. e.MarginBounds.Height/myPrintFont.GetHeight(myGraphics);  
  18.  
  19. int iLineNumber=0;//打印行數(shù)  
  20.  
  21. float fyPosition=0;//打印時的縱坐標  
  22.  
  23. float fMarginLeft=e.MarginBounds.Left;//紙頁面左邊界  
  24.  
  25. float fMarginTop=e.MarginBounds.Top;  
  26.  
  27. string strLine="";  
  28.  
  29. while ((iLineNumber
  30.  
  31. {  
  32.  
  33. fyPosition=fMarginTop+iLineNumber*myPrintFont.GetHeight(myGraphics);  
  34.  
  35. myGraphics.DrawString(strLine,myPrintFont,  
  36. new SolidBrush(Color.Black),fMarginLeft,  
  37. fyPosition,new StringFormat());  
  38.  
  39. iLineNumber++;  
  40.  
  41. }  
  42.  
  43. if (strLine!=null)  
  44.  
  45. {  
  46.  
  47. e.HasMorePages=true;  
  48.  
  49. }  
  50.  
  51. else 
  52.  
  53. {  
  54.  
  55. e.HasMorePages =false;  
  56.  
  57. }  
  58.  
  59. }  

C#打印文檔之頁面設置菜單(mnPageSetup):

 
 
 
  1. PageSetupDialog mypgDialog=new PageSetupDialog();  
  2.  
  3. mypgDialog.Document =this.MyPrintDC;  
  4.  
  5. try 
  6.  
  7. {  
  8.  
  9. mypgDialog.ShowDialog();  
  10.  
  11. }  
  12.  
  13. catch 
  14.  
  15. {  
  16.  
  17. this.MyPrintDC.PrintController.OnEndPrint(  
  18. this.MyPrintDC,new System.Drawing.Printing.PrintEventArgs());  
  19.  
  20. }  

C#打印文檔之打印預覽菜單(mnPrintView):

 
 
 
  1. PrintPreviewDialog myptViewDialog=new PrintPreviewDialog();  
  2.  
  3. myptViewDialog.Document =this.MyPrintDC;  
  4.  
  5. try 
  6.  
  7. {  
  8.  
  9. myptViewDialog.ShowDialog();  
  10.  
  11. }  
  12.  
  13. catch 
  14.  
  15. {  
  16.  
  17. this.MyPrintDC.PrintController.OnEndPrint(  
  18. this.MyPrintDC,new System.Drawing.Printing.PrintEventArgs());  
  19.  
  20. }  
  21.  
  22. 打印菜單(mnPrint):  
  23.  
  24. PrintDialog ptDialog=new PrintDialog();  
  25.  
  26. ptDialog.Document =this.MyPrintDC;  
  27.  
  28. if (ptDialog.ShowDialog()==DialogResult.OK)  
  29.  
  30. {  
  31.  
  32. try 
  33.  
  34. {  
  35.  
  36. this.MyPrintDC.Print();  
  37.  
  38. }  
  39.  
  40. catch 
  41.  
  42. {  
  43.  
  44. this.MyPrintDC.PrintController.OnEndPrint(  
  45.  
  46. this.MyPrintDC,new System.Drawing.Printing.PrintEventArgs());  
  47.  
  48. }  
  49.  
  50. }  

C#打印文檔之復制菜單(mnCopy):

 
 
 
  1. if (this.richTextBox1.SelectedText!="")  
  2.  
  3. {  
  4.  
  5. Clipboard.SetDataObject(this.richTextBox1.SelectedText);  
  6.  
  7. this.mnCopy.Enabled =false;  
  8.  
  9. this.mnCut.Enabled =false;  
  10.  
  11. this.mnPaste.Enabled =true;  
  12.  
  13. }  

C#打印文檔之剪切菜單(mnCut):

 
 
 
  1. if (this.richTextBox1.SelectedText!="")  
  2.  
  3. {  
  4.  
  5. Clipboard.SetDataObject(this.richTextBox1.SelectedText);  
  6.  
  7. this.richTextBox1.SelectedText ="";  
  8.  
  9. this.mnCopy.Enabled =false;  
  10.  
  11. this.mnCut.Enabled =false;  
  12.  
  13. this.mnPaste.Enabled =true;  
  14.  
  15. }  

C#打印文檔之粘貼菜單(mnPaste):

 
 
 
  1. IDataObject d=Clipboard.GetDataObject();  
  2.  
  3. this.richTextBox1.SelectedText =(string)d.GetData(DataFormats.Text);  

C#打印文檔之查找菜單(mnSearch):

 
 
 
  1. f.Show(); 

C#打印文檔之富文本框richTextBox1的文件選擇改變事件(SelectionChanged)

 
 
 
  1. if (this.richTextBox1.SelectedText!="")  
  2.  
  3. {  
  4.  
  5. this.mnCut.Enabled =true;  
  6.  
  7. this.mnCopy.Enabled =true;  
  8.  
  9. }  
  10.  
  11. else 
  12.  
  13. {  
  14.  
  15. this.mnCut.Enabled =false;  
  16.  
  17. this.mnCopy.Enabled =false;  
  18.  
  19. this.mnPaste.Enabled =true;  
  20.  
  21. }  

C#打印文檔4.Form2中的代碼:

定義一個整型變量:

 
 
 
  1. private int findPlace=0; 

命令按鈕"查找下一個"代碼

 
 
 
  1. if (this.txtSearch.Text !="")  
  2.  
  3. {  
  4.  
  5. Form1 mainform=(Form1)this.Owner;  
  6.  
  7. if (mainform.richTextBox1.Text.Length>0)  
  8.  
  9. {if(  
  10. (this.findPlace=  
  11. mainform.richTextBox1.Text.IndexOf(  
  12. this.txtSearch.Text,this.findPlace))==-1)  
  13.  
  14. {  
  15.  
  16. MessageBox.Show("沒有找到!");  
  17.  
  18. this.findPlace =0;  
  19.  
  20. }  
  21.  
  22. else 
  23.  
  24. {mainform.richTextBox1.Select(  
  25. this.findPlace,this.txtSearch.Text.Length);  
  26.  
  27. this.findPlace=  
  28. this.findPlace+this.txtSearch.Text.Length;  
  29.  
  30. mainform.Activate();  
  31.  
  32. }  
  33. }  
  34. }  

命令按鈕"取消"代碼:

 
 
 
  1. this.Hide();  
  2.  
  3. this.Owner.Show();  

C#打印文檔的實際操作和具體的步驟就向你介紹到這里,希望對你了解和學習C#打印文檔的實現(xiàn)有所幫助。

【編輯推薦】

  1. .NET Framework概念及開發(fā)淺析
  2. C#實現(xiàn)打印功能實例詳解
  3. 淺析C#打印和C#打印預覽的實現(xiàn)
  4. 全面解析C#實現(xiàn)打印功能
  5. 實現(xiàn)C#打印窗體實例詳解

標題名稱:實現(xiàn)C#打印文檔實例詳解
轉(zhuǎn)載來于:http://m.5511xx.com/article/dpepocd.html