现在的位置: 首页 > 综合 > 正文

C# 多页打印简单实现

2013年01月27日 ⁄ 综合 ⁄ 共 1101字 ⁄ 字号 评论关闭

必要的类:

using System.Drawing.Printing;

我的打印代码放在一个Button里:

private void button1_Click(object sender, EventArgs e)
        {                
            PrintDocument pdDocument = new PrintDocument();            
            pdDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);   
            //pdDocument.DefaultPageSettings.Landscape = true;
          //此处更改页面为横向打印 
            pdDocument.Print();
        }

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {             
            e.HasMorePages = true; //此处打开多页打印属性

            il++;             //il是一个计数器,即页数
                Bitmap bmp = new Bitmap(this.Width, this.Height);
//生成一个BMP对象
                this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
//将当前界面转成BMP图片                
                Image PrintImage = (Image)bmp.Clone();                 
//将BMP转换成Image对象
                e.Graphics.DrawImage(PrintImage, 0, 0);
//在左上角0,0的位置打印图像
            if(il>9) //共打印10张
            e.HasMorePages = false; //关掉多页打印属性
        }

多页打印必须把HasMorePages 设为true,达到需要的页数后关掉此属性。否则无穷添加新页面!

经我测试发现,当HasMorePages 设为true后,PrintDocument_PrintPage重复自我运行,直到HasMorePages 设为false。

那么更新每一页页面就成了红色代码部分的任务。

在PrintDocument_PrintPage函数内部,多次运行e.Graphics.DrawImage(PrintImage, 0, 0);是没有任何效果的,最多只能刷新当前页,不能产生新页。

 

抱歉!评论已关闭.