码迷,mamicode.com
首页 > Web开发 > 详细

.Net(c#)打印--多页打印

时间:2014-08-26 00:12:45      阅读:919      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   io   for   ar   art   代码   log   

如果要实现多页打印,就要使用PrintPageEventArgs类的HasMorePages属性。

我们对之前的代码作如下变更:
     增加PrintDocument的BeginPrint和EndPrint事件。 BeginPrint事件用于得到被打印的内容。 EndPrint用于释放资源。 PrintDocument的PrintPage事件中实现分页。
     基中:BeginPrint的事件方法在PrintPage事件方法前被呼叫。
            PintPage的事件方法在EndPrintPage事件方法前被呼叫。
            EndPrint事件方法最后被呼叫,EndPrint事件方法结束后会回到PrintDocument.Print()方法中,执行打印。

其过程如下:
     1、实例化打印文档
     2、订阅事件(订阅BeginPrint事件,用于得到被打印的内容;PinrtPage事件,用于绘制各个页内容; EndPrint事件,用于释放资源)
     3、调用BeginPrint事件的方法,得到打印内容
     4、调用PinrtPage事件的方法,绘制多个打印页面,并根据判断,设置是否进行多页打印
     5、调用EndPrint事件的方法,释放资源,完成后开始打印

代码如下:


using System.IO;
using System.Drawing.Printing;

namespace SimpleEditor
{
     public partial class SimpleEditorForm : Form
     {
         private string filename = "Untitled";

         //1、实例化打印文档
         PrintDocument pdDocument = new PrintDocument();

         private string[] lines;
         private int linesPrinted;
         public SimpleEditorForm()
         {
             InitializeComponent();

             //2、订阅事件

             //订阅PinrtPage事件,用于绘制各个页内容
             pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
             //订阅BeginPrint事件,用于得到被打印的内容
             pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
             //订阅EndPrint事件,用于释放资源
             pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);
         }

         private void OnFilePrint(object sender, EventArgs e)
         {
             try
             {
                 //调用打印
                 pdDocument.Print();

                 /*
                  * PrintDocument对象的Print()方法在PrintController类的帮助下,执行PrintPage事件。
                  */
             }
             catch (InvalidPrinterException ex )
             {
                 MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 throw;
             }
         }

         /// <summary>
         /// 3、得到打印内容
         ///每个打印任务只调用OnBeginPrint()一次。
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         void pdDocument_BeginPrint(object sender, PrintEventArgs e)
         {
             char[] param ={ ‘\n‘ };
             lines = textBoxEdit.Text.Split(param);

             int i = 0;
             char[] trimParam ={ ‘\r‘ };
             foreach (string s in lines)
             {
                 lines[i++] = s.TrimEnd(trimParam);
             }
         }




         /// <summary>
         /// 4、绘制多个打印页面
         /// printDocument的PrintPage事件
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void OnPrintPage(object sender, PrintPageEventArgs e)
         {
             /*
              *得到TextBox中每行的字符串数组
              * \n换行
              * \r回车
              */

             int x = 20;
             int y = 20;
             while (linesPrinted<lines.Length)
             {
                 //绘制要打印的页面
                 e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);

                 y += 55;

                 //判断超过一页时,允许进行多页打印
                 if (y >= e.PageBounds.Height - 80)
                 {
                     //允许多页打印
                     e.HasMorePages = true;

                     /*
                      * PrintPageEventArgs类的HaeMorePages属性为True时,通知控件器,必须再次调用OnPrintPage()方法,打印一个页面。
                      * PrintLoopI()有一个用于每个要打印的页面的序例。如果HasMorePages是False,PrintLoop()就会停止。
                      */
                     return;
                 }
             }

             linesPrinted = 0;
             //绘制完成后,关闭多页打印功能
             e.HasMorePages = false;

         }

         /// <summary>
         ///5、EndPrint事件,释放资源
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         void pdDocument_EndPrint(object sender, PrintEventArgs e)
         {
            //变量Lines占用和引用的字符串数组,现在释放
             lines = null;
         }
     }
}这样就完成了多页打印功能。

 

PageSetupDialog:
     可以配置页面大小和纸张来源、方向、页边距,因为这些选项都依赖于打印机,所以在这个对话框中也可以选择打印机。

AllowPager

AllowOrientation

PageSettings.Landscape

AllowPrinter

PageSettings.Margins

MinMagins

AllowMargins

PageSettings.PaperSource

PageSettings.PaperSize

它有一些属性如下图:

说明:

1、页面
     AllowPaper:是否可选择页面大小和纸张来源。
     PageSetupDialog.PageSetting.PaperSize属性返回一个PaperSize实例,用它的属性Height、Width和PaperName可以读取高度、宽度和纸张名称。
     PaperName指定诸如Letter和A4等名称。
     Kind属性返回一个枚举,从中可获取PaperKind枚举的一个值,PaperKind枚举包含许多不同的纸张大小,例如A3、A4、A5、Letter、LetterPlus和LetterRotated。
     PageSetupDiaog.PageSettings.PaperSource属性返回一个PaperSource实例,其中可以读取打印机纸张来源和相应的纸张类型(只要打印机用该打印机设置进行了正确的配置)。

2、页边距
     AllowMargins属性允许用户设置打印输出的页边距值。
     MinMargins可以为用户定义输入的最小页边距值.
     PageSetupDialog.PageSettings.Margins读取页边距,值有Bottom,Left,Right和Top属性。

3、方向
     AllowOrientation属性定义用户是否可以选择纵向和横向打印方式。
     PageSetupDialog.PageSettings.Landscape值可以读取选定的值。横向为True,纵向为False

4、打印机
AllowPrinter属性指定用户是否可选择打印机。



PageSetupDialog的用法

    使用PageSetupDialog的方法比较简单,只需
     1、在实例化一个PageSetupDialog
     2、设置document属性设置为需要打印的文档
     3、呼叫ShowDialog()方法,打开版面设置

bubuko.com,布布扣

.Net(c#)打印--多页打印

标签:blog   http   使用   io   for   ar   art   代码   log   

原文地址:http://www.cnblogs.com/tianguook/p/3936098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!