当我尝试使用下面的代码打印预览时,它会在之前打印的项目上打印项目。当我将if
块放在for
循环之外时,它开始生成一个无限页面。
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Black);
Font font = new Font("Courier New", 12);
float fontHeight = font.GetHeight();
int startX = 40;
int startY = 30;
int lineperpage = 0;
for (int i = 0; i < 100; i++)
{
e.Graphics.DrawString("Line: " + i, font, brush, startX, startY);
startY += font.Height;
lineperpage++;
if (lineperpage > 50)
{
e.HasMorePages = true;
startY = 30;
lineperpage = 0;
startX = 300;
}
else
{
e.HasMorePages = false;
}
}
}