c# 的多页打印 [用 PrintDocument 不用水晶报表]

发现手头代码居然是用 PrintDocument 实现的打印,而不是水晶报表. 只好学习一下了.

--------------------------------------------------

http://hi.baidu.com/jialinniao/blog/item/ef46c8fd2bdbe88ab801a052.html

学习.Net(c#)打印--多页打印
2009-04-10 16:28
如果要实现多页打印,就要使用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;
         }
     }
}这样就完成了多页打印功能。

我们虽然设置了多面打印,可是打印机无法设定页面边距等内容。那我们如何设定打印格式呢?

 

--------------------------------------------------

http://news.cnblogs.com/question/9085/

在C#后台利用PrintDocument 实现打印多页tiff 功能 ?

在C#后台利用PrintDocument 打印时候, 发现多页tif 只出来第一页内容,后面的没出来,

希望各位高手,,指点指点,, 有源代码最好,, 先谢谢拉!!!


简明的例子:

参考:

http://radio.weblogs.com/0122832/2005/10/20.html

http://stackoverflow.com/questions/748735/unable-to-preview-multiple-page-tiff-files-in-net

Code
 private Image m_Image;
    private Int32 m_CurrentPage;
    private Int32 m_PageCount;

    private void Form1_Load(object sender, EventArgs e)
    {
        m_Image = Image.FromFile(".\\Test-2-Page-Image.tif");
        m_PageCount = m_Image.GetFrameCount(FrameDimension.Page);
    }

    private void printDocument_BeginPrint(object sender, PrintEventArgs e)
    {
        m_CurrentPage = 0;
        m_PageCount = m_Image.GetFrameCount(FrameDimension.Page);
    }

    private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        m_Image.SelectActiveFrame(FrameDimension.Page, m_CurrentPage);
        e.Graphics.DrawImage(m_Image, 0, 0);
        ++m_CurrentPage;
        e.HasMorePages = (m_CurrentPage < m_PageCount);
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        printDocument.Print();
    }
}

至于后台处理,可以另起一个线程。
--------------------------------------------------

http://hi.baidu.com/jialinniao/blog/item/dc5534fb6643c416a9d31156.html

学习.Net(c#)打印--打印预览
2009-04-10 16:22
使用打印预览界面可以使用PrintPreviewDialog类
     .Net中实现打印可使用PrintPreviewControl类,该类可以用来在窗体中预览文档。但这个类没的提供相关菜单。
     PrintPreviewDialog在PrintPreviewControl有基础上封装了一些控件的对话框。它派生于System.Windows.Forms.Form。

      这里我们用PrintPreviewDialog界面进行打印预览。
    PrintPreviewDialog的用法与PageSetupDiaog及PrintDialog用法类似。即:

     1、在实例化一个PrintPreviewDialog
     2、设置document属性设置为需要打印的文档
     3、呼叫ShowDialog()方法,打开“打印”界面。

    我们对前次的代码作修改,增加预览功能。代码如下:

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

namespace SimpleEditor
{
     public partial class SimpleEditorForm : Form
     {
         private string filename = "Untitled";
         //打印文档
         PrintDocument pdDocument = new PrintDocument();

         //打印格式设置页面
         PageSetupDialog dlgPageSetup = new PageSetupDialog();

         //打印页面
         PrintDialog dlgPrint = new PrintDialog();

         //1、实例化打印预览
         PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();


         private string[] lines;
         private int linesPrinted;

         public SimpleEditorForm()
         {
             InitializeComponent();
             pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
             pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
             pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);

             //页面设置的打印文档设置为需要打印的文档
             dlgPageSetup.Document = pdDocument;

             //打印界面的打印文档设置为被打印文档
             dlgPrint.Document = pdDocument;

             //2、打印预览的打印文档设置为被打印文档
             dlgPrintPreview.Document = pdDocument;
         }

         /// <summary>
         ///打印预览按钮事件
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void OnFilePrintPreview(object sender, EventArgs e)
         {
             //3、显示打印预览界面
             dlgPrintPreview.ShowDialog();
         }

         /// <summary>
         ///页面设置
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void OnFilePageSetup(object sender, EventArgs e)
         {
             dlgPageSetup.ShowDialog();
         }

         private void OnExit(object sender, EventArgs e)
         {

         }

         /// <summary>
         ///当按下打印时,此为界面中的打印按钮事件
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void OnFilePrint(object sender, EventArgs e)
         {
             try
             {
                 //判断是否有选择文本
                 if (textBoxEdit.SelectedText != "")
                 {
                     //如果有选择文本,则可以选择"打印选择的范围"
                     dlgPrint.AllowSelection = true;
                 }
                 else
                 {
                     dlgPrint.AllowSelection = false;
                 }
                 //呼叫打印界面
                 if (dlgPrint.ShowDialog()==DialogResult.OK)
                 {

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

         /// <summary>
         ///每个打印任务只调用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);
             //判断是否选取列印被选择的范围
             if (dlgPrint.PrinterSettings.PrintRange==PrintRange.Selection)
             {
                 lines = textBoxEdit.SelectedText.Split(param);
             }
             else
             {
                 lines = textBoxEdit.Text.Split(param);
             }

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




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

             int x = e.MarginBounds.Left;
             int y = e.MarginBounds.Top;
             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>
         /// EndPrint事件释放BeginPrint方法中占用的资源
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         void pdDocument_EndPrint(object sender, PrintEventArgs e)
         {
            //变量Lines占用和引用的字符串数组,现在释放
             lines = null;
         }
     }
}



这样我们就完成了打印的全部功能。

--------------------------------------------------


http://hi.baidu.com/jialinniao/blog/item/df7194122aaea8886438db5e.html

学习.Net(c#)打印--页面设置
2009-04-10 16:35

要进行版面设置需要使用PageSetupDialog类。

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()方法,打开版面设置

如前边例子中加入版面设置,代码如下:


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

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

         //实例化打印文档
         PrintDocument pdDocument = new PrintDocument();
       
         //1、打印格式设置页面
         PageSetupDialog dlgPageSetup = new PageSetupDialog();

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

             //订阅事件

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

             //2、页面设置的打印文档设置为需要打印的文档
             dlgPageSetup.Document = pdDocument;
         }


         /// <summary>
         /// 3、在页面设置按钮事件中呼叫页面设置界面,这样单击“页面设置”按钮就会弹出“页面设置”界面
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void OnFilePageSetup(object sender, EventArgs e)
         {
             dlgPageSetup.ShowDialog();
         }

         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>
         ///得到打印内容
         ///每个打印任务只调用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>
         ///绘制多个打印页面
         /// 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>
         ///EndPrint事件,释放资源
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         void pdDocument_EndPrint(object sender, PrintEventArgs e)
         {
            //变量Lines占用和引用的字符串数组,现在释放
             lines = null;
         }
     }
}

posted @ 2013-04-03 17:14  Nina  阅读(1245)  评论(0编辑  收藏  举报