温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实现C#打印文档的步骤

发布时间:2021-08-13 21:09:19 来源:亿速云 阅读:193 作者:chen 栏目:编程语言

本篇内容主要讲解“实现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;

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

private StringReader myReader;   private Form2 f;

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

f=new Form2();   f.Owner =this;   f.Hide();

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);   }   }   }   }

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

this.CheckSave();   this.richTextBox1.Clear();   this.myStatus.Text ="新建文件";

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 ="打开文件";

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

this.myStatus.Text ="保存文件";   SaveFileDialog svfDialog=new SaveFileDialog();   svfDialog.Filter ="文本文件|*.*|富文本格式文件|*.rtf|所有文件|*.*";   if (svfDialog.ShowDialog()==DialogResult.OK)   {   this.richTextBox1.SaveFile(svfDialog.FileName,  RichTextBoxStreamType.PlainText);   }

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;   }   }

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());   }

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());   }   }

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

if (this.richTextBox1.SelectedText!="")   {   Clipboard.SetDataObject(this.richTextBox1.SelectedText);   this.mnCopy.Enabled =false;   this.mnCut.Enabled =false;   this.mnPaste.Enabled =true;   }

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;   }

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

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

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

f.Show();

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;   }

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

定义一个整型变量:

private int findPlace=0;

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

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();   }  }  }

命令按钮"取消"代码:

this.Hide();   this.Owner.Show();

到此,相信大家对“实现C#打印文档的步骤”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI