gpt4 book ai didi

c# - 如何从 WinForms 打印多页?

转载 作者:行者123 更新时间:2023-11-30 14:01:00 25 4
gpt4 key购买 nike

虽然网上有一些教程,但我仍然不明白为什么这不能正确打印多页。我做错了什么?

public static void printTest()
{
PrintDialog printDialog1 = new PrintDialog();
PrintDocument printDocument1 = new PrintDocument();

printDialog1.Document = printDocument1;
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);

DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}

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 = 40;

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;
}
else {
e.HasMorePages = false;
}
}
}

您可以在此处找到此代码打印结果的示例:Printed Document

最佳答案

你永远不会从循环中返回。将其更改为:

if (offsetY >= pageHeight)
{
e.HasMorePages = true;
offsetY = 0;
return; // you need to return, then it will go into this function again
}
else {
e.HasMorePages = false;
}

此外,您需要将循环更改为从第二页的当前编号开始,而不是再次以 i=0 重新启动。

关于c# - 如何从 WinForms 打印多页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9341284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com