实现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#打印文档之加入引用:

using System.IO; 
  • 1.

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

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

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

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

C#打印文档之Form1窗体中定义一个方法CheckSave ()

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

C#打印文档之新建菜单(mnNew):

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

C#打印文档之打开菜单(mnOpen):

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

C#打印文档之保存菜单(mnSave):

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

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

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

C#打印文档之页面设置菜单(mnPageSetup):

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

C#打印文档之打印预览菜单(mnPrintView):

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

C#打印文档之复制菜单(mnCopy):

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

C#打印文档之剪切菜单(mnCut):

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

C#打印文档之粘贴菜单(mnPaste):

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

C#打印文档之查找菜单(mnSearch):

f.Show(); 
  • 1.

C#打印文档之富文本框richTextBox1的文件选择改变事件(SelectionChanged)

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

C#打印文档4.Form2中的代码:

定义一个整型变量:

private int findPlace=0; 
  • 1.

命令按钮"查找下一个"代码

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

命令按钮"取消"代码:

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

C#打印文档的实际操作和具体的步骤就向你介绍到这里,希望对你了解和学习C#打印文档的实现有所帮助。

【编辑推荐】

  1. .NET Framework概念及开发浅析
  2. C#实现打印功能实例详解
  3. 浅析C#打印和C#打印预览的实现
  4. 全面解析C#实现打印功能
  5. 实现C#打印窗体实例详解
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-26 11:07:36

C#打印窗体

2009-08-26 09:22:44

C#实现打印功能

2009-08-26 13:22:24

C#打印程序

2009-08-26 12:59:08

C#打印设置

2009-08-21 10:13:02

C#异步初步

2009-08-26 11:53:56

C#打印文本文件

2009-09-09 12:55:59

C# TextBox事

2009-08-26 12:14:44

C#打印设置

2009-09-04 14:14:55

C#文档

2009-08-20 11:01:51

C#操作内存

2009-08-18 10:14:19

C#插件构架

2009-09-11 12:31:52

C#实例详解TypeConvert

2009-09-01 13:51:51

C#创建Word文档

2009-08-12 15:26:38

C#读取XML文档

2009-09-01 13:13:28

C#打开Word文档

2009-09-02 17:12:06

C#关机代码

2009-08-28 17:34:14

读取word文档

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-26 14:31:08

C#打印文件

2009-08-28 13:12:56

C#反射实例C#反射
点赞
收藏

51CTO技术栈公众号