实现C#打印文档实例详解

  • 实现C#打印文档所用到的类是什么呢?实现C#打印文档具体的步骤又是什么呢?那么本文就向你介绍C#打印文档详细的内容。
  •  

    我们在实际开发中会遇到实现C#打印文档的需求,那么如何设计一个编辑处理程序呢,它可以实现编辑和打印、打印预览文档。那么下面我们就详细向你介绍C#打印文档的具体的操作和实现。

    C#打印文档操作方式:

    C#打印文档1.新建一个项目

    项目中有两个form(Form1,Form2)

    C#打印文档2.在Form1中添加菜单

    mainMenu1,一个richTextBox1(定义为Public),一个打印文档控件PrintDocument,名称为MyPrintDC。一个状态栏名称为myStatus。

    菜单项有:

    文件(mnFile){新建(mnNew),打开(mnOpen),保存(mnSave),页面设置(mnPageSetup),打印预览(mnPrintView),打印(mnPint),退出(mnClose)}

    编辑(mnEdit){复制(mnCopy),剪切(mnCut),粘贴(mnPaste),查找(mnSearch)}

    关于(mnAbout)

    C#打印文档3.在Form2中添加一个标签:

    查找内容,文本(txtSearch),命令按钮(btnSearch) 查找一下个,命令按钮(btnCancel)取消4.Form1中代码:

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

      
      
    1. using System.IO; 

    C#打印文档之在控件定义阶段中加入:

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

    C#打印文档之Form1窗体的构造函数中:

      
      
    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. //计算一页行数  
    15.  
    16. float iLinePage=  
    17. e.MarginBounds.Height/myPrintFont.GetHeight(myGraphics);  
    18.  
    19. int iLineNumber=0;//打印行数  
    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<iLinePage)&&(strLine=myReader.ReadLine())!=null)  
    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#打印文档的实现有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值