C#实现打印功能是通过使用PrintDialog控件来实现的。任何物有所值的应用程序都会拥有某种打印功能,不管是基本的打印功能还是更为复杂的打印功能,比如允许用户只打印所选的文本或某个范围内的页面。本节将探讨一下实现基本的C#打印功能,看看哪些类有助于打印文件中的文本。C#实现打印功能的过程是:在调用PrintDialog控件的ShowDialog方法之前,必须先设置PrintDialog类的Document属性。该属性接受一个PrintDocument类,以获得打印机设置并将输出内容发送给打印机。PrintDocument类需要System.Drawing.Printing名称空间,因此,在定义使用PrintDocument类的对象前,必须包含这个名称空间。
C#实现打印功能具体的操作步骤如下:
创建一个PrintDialog的实例。如下:
- System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();
创建一个PrintDocument的实例.如下:
- System.Drawing.Printing.PrintDocument docToPrint =
- new System.Drawing.Printing.PrintDocument();
设置打印机开始打印的事件处理函数.函数原形如下:
- void docToPrint_PrintPage(object sender,
- System.Drawing.Printing.PrintPageEventArgs e)
将事件处理函数添加到PrintDocument的PrintPage事件中。
- docToPrint.PrintPage+=
- new PrintPageEventHandler(docToPrint_PrintPage);
设置PrintDocument的相关属性,如:
- PrintDialog1.AllowSomePages =
- true;PrintDialog1.ShowHelp = true;
把PrintDialog的Document属性设为上面配置好的PrintDocument的实例:
- PrintDialog1.Document = docToPrint;
调用PrintDialog的ShowDialog函数显示打印对话框:
- DialogResult result = PrintDialog1.ShowDialog();
根据用户的选择,开始打印:
- if (result==DialogResult.OK)
- {
- docToPrint.Print();
- }
C#实现打印功能的实例如下:
使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);
- using System;
- using System.Drawing.Printing;
- using System.Windows.Forms;
- using System.IO;
- namespace EDImageSystem
- {
- /// <summary>
- /// PrintService 的摘要说明。
- /// </summary>
- public class PrintService
- {
- public PrintService()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- this.docToPrint.PrintPage+=
- new PrintPageEventHandler(docToPrint_PrintPage);
- }//将事件处理函数添加到PrintDocument的PrintPage中
- // Declare the PrintDocument object.
- private System.Drawing.Printing.PrintDocument docToPrint =
- new System.Drawing.Printing.PrintDocument();
- //创建一个PrintDocument的实例
- private System.IO.Stream streamToPrint;
- string streamType;
- // This method will set properties on the PrintDialog object and
- // then display the dialog.
- public void StartPrint(Stream streamToPrint,string streamType)
- {
- this.streamToPrint=streamToPrint;
- this.streamType=streamType;
- // Allow the user to choose the page range he or she would
- // like to print.
- System.Windows.Forms.PrintDialog PrintDialog1=
- new PrintDialog ();//实现C#打印之创建一个PrintDialog的实例。
- PrintDialog1.AllowSomePages = true;
- // Show the help button.
- PrintDialog1.ShowHelp = true;
- // Set the Document property to the PrintDocument for
- // which the PrintPage Event has been handled. To display the
- // dialog, either this property or the PrinterSettings property
- // must be set
- PrintDialog1.Document = docToPrint;
- //把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
- DialogResult result = PrintDialog1.ShowDialog();
- //调用PrintDialog的ShowDialog函数显示打印对话框
- // If the result is OK then print the document.
- if (result==DialogResult.OK)
- {
- docToPrint.Print();//实现C#打印之开始打印
- }
- }
- // The PrintDialog will print the document
- // by handling the document's PrintPage event.
- private void docToPrint_PrintPage(object sender,
- System.Drawing.Printing.PrintPageEventArgs e)
- //设置打印机开始打印的事件处理函数
- {
- // Insert code to render the page here.
- // This code will be called when the control is drawn.
- // The following code will render a simple
- // message on the printed document
- switch(this.streamType)
- {
- case "txt":
- string text = null;
- System.Drawing.Font printFont = new System.Drawing.Font
- ("Arial", 35, System.Drawing.FontStyle.Regular);
- // Draw the content.
- System.IO.StreamReader streamReader=
- new StreamReader(this.streamToPrint);
- text=streamReader.ReadToEnd();
- e.Graphics.DrawString(text,printFont,
- System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
- break;
- case "image":
- System.Drawing.Image image=
- System.Drawing.Image.FromStream(this.streamToPrint);
- int x=e.MarginBounds.X;
- int y=e.MarginBounds.Y;
- int width=image.Width;
- int height=image.Height;
- if((width/e.MarginBounds.Width)>(
- height/e.MarginBounds.Height))
- {
- width=e.MarginBounds.Width;
- height=image.Height*e.MarginBounds.Width/image.Width;
- }
- else
- {
- height=e.MarginBounds.Height;
- width=image.Width*e.MarginBounds.Height/image.Height;
- }
- System.Drawing.Rectangle destRect=
- new System.Drawing.Rectangle(x,y,width,height);
- e.Graphics.DrawImage(image,
- destRect,0,0,image.Width,image.Height,
- System.Drawing.GraphicsUnit.Pixel);
- break;
- default:
- break;
- }
- }
- }
- }
实现C#打印的具体实现步骤和具体的实例演示就向你介绍到这里,希望对你了解实现C#打印以及学习C#有所帮助。
【编辑推荐】