C# 多页打印简单实现
必要的類:
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);是沒有任何效果的,最多只能刷新當前頁,不能產生新頁。
?
總結
以上是生活随笔為你收集整理的C# 多页打印简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8086汇编与c++编译器就内存方面的感
- 下一篇: 解决:Chrome 插件安装时提示 程序