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#有所帮助。

     

     

    转自:http://developer.51cto.com/art/200908/146843.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值