我在多页中打印信息时遇到问题,目前它导致无限循环我在网上冲浪一些时间来寻求解决方案,但不是很清楚。
static void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphic = e.Graphics;
SolidBrush brush = new SolidBrush(Color.Black);
Font font = new Font("Courier New", 12);
e.PageSettings.PaperSize = new PaperSize("A4", 850, 1100);
float pageWidth = e.PageSettings.PrintableArea.Width;
float pageHeight = e.PageSettings.PrintableArea.Height;
float fontHeight = font.GetHeight();
int startX = 40;
int startY = 30;
int offsetY = 0;
for (int i = 0; i < 100; i++)
{
graphic.DrawString("Line: " + i, font, brush, startX, startY + offsetY);
offsetY += (int)fontHeight;
if (offsetY >= pageHeight)
{
e.HasMorePages = true;
offsetY = 0;
return;
}
else
{
e.HasMorePages = false;
}
}
}
任何想法? 谢谢
答案 0 :(得分:2)
好吧,我想,我想出你想要什么。我想你要打印100行。
对于您的情况,您应该使用和实例字段来保持打印行数。
尝试这样的事情:
var printedLines = 0;
var linesToPrint = 100;
...
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.HasMorePages = false;
....
while(printedLines < linesToPrint)
{
graphic.DrawString("Line: " + printedLines, font, brush, startX, startY + offsetY);
offsetY += (int)fontHeight;
++printedLines;
if (offsetY >= pageHeight)
{
e.HasMorePages = true;
offsetY = 0;
return;
}
}
}