标签:
原文:C#实现打印与打印预览功能在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的方式提供,为程序员提供了很大的方便,但是这几个组件的使用还是很复杂的,有必要解释一下。   
打印操作通常包括以下四个功能:    
1 打印设置 设置打印机的一些参数,比如更改打印机驱动程序等;     
2 页面设置 设置页面大小纸张类型等      
3 打印预览 类似于word中的打印预览      
4 打印
实现打印功能的核心是PrintDocument类这个类属于System.Drawing.Printing名字空间这个类封装了当前的打印设置页面设置以及所有的与打印有关的事件和方法   
这个类包括以下几个属性,事件和方法    
1、PrinterSettings 属性    
  存放打印机的设置信息,这个属性不需要程序员设置,因为它是由打印对话框获取的.    
2、PrintCountroller 属性    
  控制打印过程    
3、DefaultPageSettings 属性    
  存放页面设置信息,打印纸大小方向等,也不需要程序员设置,因为它是由页面设置对话框获取的.    
4、DocumentName 属性    
  指定文档名称,出现在打印机状态窗口中    
1。 BeginPrint事件   
  在打印之前发出    
2. PrintPage事件    
  每打印一页是发出,事件接受一个PrintPageEventArgs参数该参数封装了打印相关的信息
PrintPageEventArgs参数有很多重要的属性   
  1 Cancel 取消打印    
  2 Graphics 页面的绘图对象    
  3 HasMorePages 是否还有要打印的页面
   
Print方法:该方法没有参数 调用它将按照当前设置开始打印.    
若实现打印功能首先构造PrintDocument对象添加打印事件    
    
   1: PrintDocument printDocument;
      2: private void InitializeComponent()
   3: {
         4:     ...
      5: // 这里的printDocument对象可以通过将PrintDocument控件拖放到窗体上来实现,注意要设置该控件的PrintPage事件。
6: printDocument=new PrintDocument();
7: printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);
   8: ...
         9: }
  
实现打印事件功能
  
打印和绘图类似都是调用Graphics 类的方法进行画图 不同的是一个在显示器上一个在打印纸上并且打印要进行一些复杂的计算
  
如换行、分页等。
  
  
1: private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
   2: {
      3: Graphics g = e.Graphics; //获得绘图对象
4: float linesPerPage = 0; //页面的行号
5: float yPosition = 0; //绘制字符串的纵向位置
6: int count = 0; //行计数器
7: float leftMargin = e.MarginBounds.Left; //左边距
8: float topMargin = e.MarginBounds.Top; //上边距
9: string line = null; 行字符串
10: Font printFont = this.textBox.Font; //当前的打印字体
11: SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
12: linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数
13: //逐行的循环打印一页
14: while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))
  15:     {
        16:        yPosition = topMargin + (count * printFont.GetHeight(g));
      17: g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
  18:        count++;
        19:     }
      20: // 注意:使用本段代码前,要在该窗体的类中定义lineReader对象:
21: // StringReader lineReader = null;
22: //如果本页打印完成而line不为空,说明还有没完成的页面,这将触发下一次的打印事件。在下一次的打印中lineReader会
23: //自动读取上次没有打印完的内容,因为lineReader是这个打印方法外的类的成员,它可以记录当前读取的位置
24: if(line != null)
25: e.HasMorePages = true;
26: else
  27:  {
      28: e.HasMorePages = false;
29: // 重新初始化lineReader对象,不然使用打印预览中的打印按钮打印出来是空白页
30: lineReader = new StringReader(textBox.Text); // textBox是你要打印的文本框的内容
  31:  }
        32: }
  
打印设置,构造打印对话框 将对话框中设置的Document属性赋给printDocument这样会将用户的设置自动保存到printDocument
  
的PrinterSettings属性中
  
  
1: protected void FileMenuItem_PrintSet_Click(object sender,EventArgs e)
   2: {
      3: PrintDialog printDialog = new PrintDialog();
   4:     printDialog.Document = printDocument;
         5:     printDialog.ShowDialog();
         6: }
  
页面设置和打印预览与打印设置原理相同都是构造对话框将用户在对话框中的设置保存到相应的类的属性中
  
  
1: protected void FileMenuItem_PageSet_Click(object sender,EventArgs e)
   2: {
      3: PageSetupDialog pageSetupDialog = new PageSetupDialog();
   4:     pageSetupDialog.Document = printDocument;
         5:     pageSetupDialog.ShowDialog();
         6: }
  
打印预览
  
1: protected void FileMenuItem_PrintView_Click(object sender,EventArgs e)
   2: {
    3: PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
   4:    printPreviewDialog.Document = printDocument;
    5: lineReader = new StringReader(textBox.Text);
6: try
   7:    {
       8:         printPreviewDialog.ShowDialog();
       9:    }
    10: catch(Exception excep)
  11:    {
    12: MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
  13:    }
      14: }
  
打印就可以直接调用printDocument的Print()方法因为用户可能在打印之前还要再更改打印设置所以
  
在这里再次显示打印设置对话框
  
  
1: protected void FileMenuItem_Print_Click(object sender,EventArgs e)
   2:  {
      3: PrintDialog printDialog = new PrintDialog();
   4:   printDialog.Document = printDocument;
      5: lineReader = new StringReader(textBox.Text);
6: if (printDialog.ShowDialog() == DialogResult.OK)
   7:   {
      8: try
   9:       {
        10:         printDocument.Print();
        11:       }
      12: catch(Exception excep)
  13:       {
      14: MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
15: printDocument.PrintController.OnEndPrint(printDocument,new PrintEventArgs());
  16:       }
        17:    }
        18:  }
总结打印的过程是
  
1 在应用程序窗体初始化时构造PrintDocument对象,添加 printDocument 的 PrintPage 方法
  
2 实现PrintPage方法
  
3 在用户的单击事件中调用 printDocument 的 Print方法实现打印功能
  
在这中间可能要用到  PrintDialog PrintPreviewDialog PageSetupDialog 设置和查看打印效果这些方法通常是由菜单的单击触发的。
本文来自: 中科软件园(www.4oa.com) 详细出处参考:http://www.4oa.com/Article/html/6/34/501/2005/18114.html
  
作者做了部分修改和注释。
标签:
原文地址:http://www.cnblogs.com/lonelyxmas/p/5215732.html