實(shí)現(xiàn)C#打印文檔實(shí)例詳解
我們?cè)趯?shí)際開(kāi)發(fā)中會(huì)遇到實(shí)現(xiàn)C#打印文檔的需求,那么如何設(shè)計(jì)一個(gè)編輯處理程序呢,它可以實(shí)現(xiàn)編輯和打印、打印預(yù)覽文檔。那么下面我們就詳細(xì)向你介紹C#打印文檔的具體的操作和實(shí)現(xiàn)。
C#打印文檔操作方式:
C#打印文檔1.新建一個(gè)項(xiàng)目
項(xiàng)目中有兩個(gè)form(Form1,Form2)
C#打印文檔2.在Form1中添加菜單
mainMenu1,一個(gè)richTextBox1(定義為Public),一個(gè)打印文檔控件PrintDocument,名稱為MyPrintDC。一個(gè)狀態(tài)欄名稱為myStatus。
菜單項(xiàng)有:
文件(mnFile){新建(mnNew),打開(kāi)(mnOpen),保存(mnSave),頁(yè)面設(shè)置(mnPageSetup),打印預(yù)覽(mnPrintView),打印(mnPint),退出(mnClose)}
編輯(mnEdit){復(fù)制(mnCopy),剪切(mnCut),粘貼(mnPaste),查找(mnSearch)}
關(guān)于(mnAbout)
C#打印文檔3.在Form2中添加一個(gè)標(biāo)簽:
查找內(nèi)容,文本(txtSearch),命令按鈕(btnSearch) 查找一下個(gè),命令按鈕(btnCancel)取消4.Form1中代碼:
C#打印文檔之加入引用:
- using System.IO;
C#打印文檔之在控件定義階段中加入:
- private StringReader myReader;
- private Form2 f;
C#打印文檔之Form1窗體的構(gòu)造函數(shù)中:
- f=new Form2();
- f.Owner =this;
- f.Hide();
C#打印文檔之Form1窗體中定義一個(gè)方法CheckSave ()
- private void CheckSave()
- {
- if (this.richTextBox1.Text!="")
- {
- if (MessageBox.Show("是否保存當(dāng)前文件?","確認(rèn)",
- 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#打印文檔之打開(kāi)菜單(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 ="打開(kāi)文件";
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)
- {
- //打印文檔打印頁(yè)面事件代碼
- this.myReader=new StringReader(this.richTextBox1.Text);//定義字符讀流
- Graphics myGraphics=e.Graphics;
- Font myPrintFont=this.richTextBox1.Font;
- //計(jì)算一頁(yè)行數(shù)
- float iLinePage=
- e.MarginBounds.Height/myPrintFont.GetHeight(myGraphics);
- int iLineNumber=0;//打印行數(shù)
- float fyPosition=0;//打印時(shí)的縱坐標(biāo)
- float fMarginLeft=e.MarginBounds.Left;//紙頁(yè)面左邊界
- 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#打印文檔之頁(yè)面設(shè)置菜單(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#打印文檔之打印預(yù)覽菜單(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#打印文檔之復(fù)制菜單(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中的代碼:
定義一個(gè)整型變量:
- private int findPlace=0;
命令按鈕"查找下一個(gè)"代碼
- 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("沒(méi)有找到!");
- 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#打印文檔的實(shí)際操作和具體的步驟就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#打印文檔的實(shí)現(xiàn)有所幫助。
【編輯推薦】