C#实现打印功能实例详解

开发 后端
C#实现打印功能是通过什么来实现的?C#实现打印功能的步骤是什么呢?那么本文就向你介绍这方面的内容。

C#实现打印功能是通过使用PrintDialog控件来实现的。任何物有所值的应用程序都会拥有某种打印功能,不管是基本的打印功能还是更为复杂的打印功能,比如允许用户只打印所选的文本或某个范围内的页面。本节将探讨一下实现基本的C#打印功能,看看哪些类有助于打印文件中的文本。C#实现打印功能的过程是:在调用PrintDialog控件的ShowDialog方法之前,必须先设置PrintDialog类的Document属性。该属性接受一个PrintDocument类,以获得打印机设置并将输出内容发送给打印机。PrintDocument类需要System.Drawing.Printing名称空间,因此,在定义使用PrintDocument类的对象前,必须包含这个名称空间。

C#实现打印功能具体的操作步骤如下:

创建一个PrintDialog的实例。如下:

  1. System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();  

创建一个PrintDocument的实例.如下:

  1. System.Drawing.Printing.PrintDocument docToPrint =   
  2.  new System.Drawing.Printing.PrintDocument();  

设置打印机开始打印的事件处理函数.函数原形如下:

  1. void docToPrint_PrintPage(object sender,   
  2.  System.Drawing.Printing.PrintPageEventArgs e)  

将事件处理函数添加到PrintDocument的PrintPage事件中。

  1. docToPrint.PrintPage+=  
  2.  
  3. new PrintPageEventHandler(docToPrint_PrintPage);   

设置PrintDocument的相关属性,如:

  1. PrintDialog1.AllowSomePages =   
  2.  
  3. true;PrintDialog1.ShowHelp = true;   

把PrintDialog的Document属性设为上面配置好的PrintDocument的实例:

  1. PrintDialog1.Document = docToPrint;  

调用PrintDialog的ShowDialog函数显示打印对话框:

  1. DialogResult result = PrintDialog1.ShowDialog();  

根据用户的选择,开始打印:

  1. if (result==DialogResult.OK)  
  2.  {  
  3. docToPrint.Print();  
  4.  } 

C#实现打印功能的实例如下:

使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);

  1. using System;  
  2. using System.Drawing.Printing;  
  3. using System.Windows.Forms;  
  4. using System.IO;   
  5.  
  6. namespace EDImageSystem  
  7. {  
  8.  /// <summary>  
  9.  /// PrintService 的摘要说明。  
  10.  /// </summary>  
  11.  public class PrintService  
  12.  {  
  13. public PrintService()  
  14. {  
  15.  //  
  16.  // TODO: 在此处添加构造函数逻辑  
  17.  //  
  18.  this.docToPrint.PrintPage+=  
  19. new PrintPageEventHandler(docToPrint_PrintPage);  
  20. }//将事件处理函数添加到PrintDocument的PrintPage中   
  21.  
  22. // Declare the PrintDocument object.  
  23. private System.Drawing.Printing.PrintDocument docToPrint =   
  24.  new System.Drawing.Printing.PrintDocument();  
  25. //创建一个PrintDocument的实例   
  26.  
  27. private System.IO.Stream streamToPrint;  
  28. string streamType;   
  29.  
  30. // This method will set properties on the PrintDialog object and  
  31. // then display the dialog.  
  32. public void StartPrint(Stream streamToPrint,string streamType)  
  33. {   
  34.  
  35.  this.streamToPrint=streamToPrint;  
  36.  this.streamType=streamType;  
  37.  // Allow the user to choose the page range he or she would  
  38.  // like to print.  
  39.  System.Windows.Forms.PrintDialog PrintDialog1=  
  40. new PrintDialog ();//实现C#打印之创建一个PrintDialog的实例。  
  41.  PrintDialog1.AllowSomePages = true;   
  42.  
  43.  // Show the help button.  
  44.  PrintDialog1.ShowHelp = true;   
  45.  
  46.  // Set the Document property to the PrintDocument for   
  47.  // which the PrintPage Event has been handled. To display the  
  48.  // dialog, either this property or the PrinterSettings property   
  49.  // must be set   
  50.  PrintDialog1.Document = docToPrint;  
  51. //把PrintDialog的Document属性设为上面配置好的PrintDocument的实例   
  52.  
  53.  DialogResult result = PrintDialog1.ShowDialog();  
  54. //调用PrintDialog的ShowDialog函数显示打印对话框   
  55.  
  56.  // If the result is OK then print the document.  
  57.  if (result==DialogResult.OK)  
  58.  {  
  59. docToPrint.Print();//实现C#打印之开始打印  
  60.  }   
  61.  
  62. }   
  63.  
  64. // The PrintDialog will print the document  
  65. // by handling the document's PrintPage event.  
  66. private void docToPrint_PrintPage(object sender,   
  67.  System.Drawing.Printing.PrintPageEventArgs e)  
  68. //设置打印机开始打印的事件处理函数  
  69. {   
  70.  
  71.  // Insert code to render the page here.  
  72.  // This code will be called when the control is drawn.   
  73.  
  74.  // The following code will render a simple  
  75.  // message on the printed document  
  76.  switch(this.streamType)  
  77.  {  
  78. case "txt":  
  79.  string text = null;  
  80.  System.Drawing.Font printFont = new System.Drawing.Font  
  81. ("Arial", 35, System.Drawing.FontStyle.Regular);   
  82.  
  83.  // Draw the content.  
  84.  System.IO.StreamReader streamReader=  
  85. new StreamReader(this.streamToPrint);  
  86.  text=streamReader.ReadToEnd();  
  87.  e.Graphics.DrawString(text,printFont,  
  88. System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);  
  89.  break;  
  90. case "image":  
  91.  System.Drawing.Image image=  
  92. System.Drawing.Image.FromStream(this.streamToPrint);  
  93.  int x=e.MarginBounds.X;  
  94.  int y=e.MarginBounds.Y;  
  95.  int width=image.Width;  
  96.  int height=image.Height;  
  97.  if((width/e.MarginBounds.Width)>(  
  98. height/e.MarginBounds.Height))  
  99.  {  
  100. width=e.MarginBounds.Width;  
  101. height=image.Height*e.MarginBounds.Width/image.Width;  
  102.  }  
  103.  else 
  104.  {  
  105. height=e.MarginBounds.Height;  
  106. width=image.Width*e.MarginBounds.Height/image.Height;  
  107.  }  
  108.  System.Drawing.Rectangle destRect=  
  109. new System.Drawing.Rectangle(x,y,width,height);  
  110.  e.Graphics.DrawImage(image,  
  111. destRect,0,0,image.Width,image.Height,  
  112. System.Drawing.GraphicsUnit.Pixel);  
  113.  break;  
  114. default:  
  115.  break;  
  116.  }  
  117.    
  118. }   
  119.  
  120. }  
  121. }  

实现C#打印的具体实现步骤和具体的实例演示就向你介绍到这里,希望对你了解实现C#打印以及学习C#有所帮助。

【编辑推荐】

  1. 创建C#串口通信程序详解
  2. 详解C#串口监听的实现
  3. C#入门之概念简介
  4. C#入门之C#特点浅析
  5. .NET Framework概念及开发浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-26 11:32:37

C#打印文档

2009-08-26 11:07:36

C#打印窗体

2009-08-26 10:43:14

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-09-03 19:00:15

C#判断浏览器

2009-09-03 14:55:56

C#实现DataGri

2009-08-26 12:14:44

C#打印设置

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-26 14:31:08

C#打印文件

2009-09-02 17:12:06

C#关机代码

2009-08-20 11:01:51

C#操作内存

2009-08-18 10:14:19

C#插件构架

2009-09-11 12:31:52

C#实例详解TypeConvert

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 12:47:30

C#静态方法应用

2009-08-26 13:36:33

C#打印控件
点赞
收藏

51CTO技术栈公众号