用C#的printdocument写数据,要求每页只写入31排,写满一页后,将多的内容写到第二页,以此类推,该如何写?
请根据我写的代码进行修改,怎样使我的长字符串str显示在多页上?
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
string str = "Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example," +
"Hello,World!,This,is,a,multi - page,print,example,";
string[] arr = str.Split(',');
// 定义变量表示当前页的编号、每页的行数和当前已经打印的行数
int page = 1;
int rowsPerPage = 31;
int rowsPrinted = 0;
int totalPages = (arr.Length + 30) / 31;
// 创建用于绘制文本的字体
Font font = new Font("Arial", 10);
// 循环打印每一页
while (page <= totalPages)
{
//循环结束后显示的行数清零
int row = 0;
// 绘制当前页的内容
for (int i = rowsPrinted; i < arr.Length; i++)
{
e.Graphics.DrawString(arr[i], font, Brushes.Black, new PointF(615, 215 + row * 18));
rowsPrinted++;
row++;
// 如果已经绘制的行数达到了每页的行数,跳出循环
if (rowsPrinted >= rowsPerPage * page)
{
break;
}
}
page++;
//显示页码
e.Graphics.DrawString(page.ToString(), font, Brushes.Black, new PointF(615, 215 + 32 * 18));
// 如果还有更多的页面要打印,设置HasMorePages为true
if (page <= totalPages)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
}
private void 打印预览_Click(object sender, EventArgs e)
{
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument1;
printPreviewDialog.ShowDialog();
}