在我致力于疯人院之前,我想尝试一下:如何编写代码以打印多页?
我一直在尝试可以在stackoverflow(和其他地方)上找到的所有示例,但没有成功!这确实使我比以前更疯狂!我发现的所有其他示例都在处理与我尝试执行的操作无关的问题。我尝试修复的示例将在两页上打印0-100个整数,即在第1页上显示0-80,在第2页上打印81-100。尽管建议的所有技术,但我能得到的只是一页被页面覆盖的信息。 2的数据位于顶部。
e.HasMorePages = true; 应该开始下一页,但是不起作用。
为此,我创建了一个非常简单的Winform程序,下面是代码。任何想法将不胜感激:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace PrintMultiplePages_v2
{
public partial class Form1 : Form
{
ArrayList al = new ArrayList();
private int fontcount;
private int fontposition = 1;
private float ypos;
private string textToPrint;
private PrintPreviewDialog previewDlg = null;
private PrintDocument pd = null;
private int counter = 0;
private int amtperpage = 80; // The amount of lines per page
public Form1()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
al.Add(i.ToString());
}
private void buttonPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float leftMargin = 70.0f;
float topMargin = 20.0f;
float lineInc = 20.0f;
Font printFontArial10 = new Font("Arial", 10, FontStyle.Regular);
Graphics g = e.Graphics;
double pageCount = (double)al.Count / (double)amtperpage;
int pageRequired = Convert.ToInt32(Math.Ceiling(pageCount));
counter = 0;
for (int page = 1; page <= pageRequired; page++)
{
int counterMax = amtperpage * page;
if (counterMax > al.Count)
counterMax = al.Count;
for (int x = counter; x < counterMax; x++)
{
textToPrint = al[x].ToString() + " - test";
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin, topMargin + lineInc);
lineInc += 12;
counter++;
}
if (counter == counterMax)
{
if (counter != al.Count)
{
e.HasMorePages = true;
counter++;
lineInc = 20.0f;
}
}
else
e.HasMorePages = false;
}
}
}
}
正确的代码是:
private int page = 0;
private void buttonPrint_Click(object sender, EventArgs e)
{
page = 0;
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float leftMargin = 70.0f;
float topMargin = 20.0f;
float lineInc = 20.0f;
Font printFontArial10 = new Font("Arial", 10, FontStyle.Regular);
Graphics g = e.Graphics;
int stop = counter + amtperpage;
if (stop > al.Count)
stop = al.Count;
while (counter < stop)
{
textToPrint = al[counter].ToString() + " - test";
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin, topMargin + lineInc);
lineInc += 12;
counter++;
}
page++;
e.HasMorePages = counter < al.Count;
}
应该重复调用PrintPage事件,直到e.HasMorePages变为false。该事件一次只能打印一页。您只有一次调用它,并且在一个for循环中将两个页面都输入了它。换句话说,for循环正在杀死您。从逻辑上讲,您应该跟踪当前位于哪个页面上(pd_PrintPage之外),并在计数器继续计数时递增。你可以告诉你有这样的错误,因为计数器被设定为零的pd_PrintPage而被应在buttonPrint_Click设置为零。
因此,从pd_PrintPage中拉出“ int页面”,并使循环类似于
int stop = counter + amtperpage;
if (stop >= al.Count)
stop = al.Count - 1; // - 1 to prevent index out of range error.
while (counter <= stop)
{
textToPrint = al[counter].ToString() + " - test";
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin, topMargin + lineInc);
lineInc += 12;
counter++;
}
page++;
e.HasMorePages = counter < al.Count - 1; // pesky zero-based array issue again.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句