用C#的printdocument写数据,要求每页只写入31排,写满一页后,将多的内容写到第二页,以此类推,该如何写?别人也给了我方法,我试了一下,始终有问题,所有内容都会显示在第一页上,不会将多于31排的内容自动显示到后面的页上。
没有超过31排的,显示没有问题。
超过31排,也就是数据超过一页的,就不能正常显示了
有人说用e.HasMorePages = true来解决,但是自己理解的不是很透彻,如果内容超过了31排,也就是超过了一页,用了e.HasMorePages = true后,内容会重复显示在第一页,没有自动走纸,将多于31排的内容显示到后面的页上,代码如下:
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();
}
e.HasMorePages要设置成true,则表示新建一页,什么时候它变成false,表示最后一页
你应该先设置e.HasMorePages,后page++,否则page先变成了2,你的e.HasMorePages就会设置成false
望采纳!!!
你可以使用 PrintDocument 类来实现。
PrintDocument 类提供了一系列的事件,可以让你在打印过程中获得通知,从而可以在这些事件中实现打印多页的功能。
你可以在 PrintDocument 的 PrintPage 事件中编写代码,根据 e.HasMorePages 属性的值来决定是否继续打印下一页。
举个例子,假设你有一个类似于以下的代码:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// 绘制当前页面的内容
// ...
// 决定是否继续打印下一页
if (morePagesToPrint)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
你可以在 morePagesToPrint 变量中保存当前是否还有需要打印的页面,然后设置 e.HasMorePages 属性的值来决定是否继续打印下一页。
如果你想在预览中显示多页内容,你可以使用 PrintPreviewDialog 控件来实现。PrintPreviewDialog 控件可以让你使用 PrintDocument 对象来打印多页的内容,并在预览中显示出来。
下面是一个简单的例子,展示了如何使用 PrintPreviewDialog 控件来预览多页内容:
// 定义一个 PrintDocument 对象
PrintDocument printDocument = new PrintDocument();
// 设置打印页面的事件处理程序
printDocument.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// 创建一个 PrintPreviewDialog 对象
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
// 将 PrintDocument 对象关联到 PrintPreviewDialog
printPreviewDialog.Document = printDocument;
// 显示 PrintPreviewDialog
printPreviewDialog.ShowDialog();
谢邀,望采纳!!!
你可以使用 PrintDocument 类来实现,必要的类:
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,达到需要的页数后关掉此属性。否则无穷添加新页面!
参考该实例【C# PrintDocument打印 多页 打印预览】,链接:https://www.cnblogs.com/smallsoftfox/archive/2012/06/25/2562723.html
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; //关掉多页打印属性
}
最好看一下官方的api,具体的属性设置按照官方的来
https://www.shuzhiduo.com/A/rV57XD7qdP/
请根据我写的代码进行修改,怎样使我的长字符串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();
}