如果要實現多頁打印,就要使用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;
}
}
}
這樣就完成了多頁打印功能。
我們對之前的代碼作如下變更:
增加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;
}
}
}
我們雖然設置了多面打印,可是打印機無法設定頁面邊距等內容。那我們如何設定打印格式呢?
請看下頁:學習.Net(c#)打印--頁面設置
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述