gpt4 book ai didi

c# - 基于 C# 中使用的坐标,使用 System.Drawing.Printing 在多个页面上打印图形

转载 作者:行者123 更新时间:2023-11-30 18:04:23 25 4
gpt4 key购买 nike

使用 System.Drawing.Printing,我想在打印文档上打印一组线条。但问题是它会在第一页上打印每一行,不管坐标是什么,如果我画一个图像,它会在一个页面上打印它,不管它有多大。

以下是我在多个页面上打印文本所做的:

protected void ThePrintDocument_PrintPage (object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = this.richTextBox1.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);

// Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

// Iterate over the string using the StringReader, printing each line.
while(count < linesPerPage && ((line=myReader.ReadLine()) != null))
{
// calculate the next line position based on
// the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));

// draw the next line in the rich edit control

ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}

// If there are more lines, print another page.
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;

myBrush.Dispose();

}

为了在多页上打印行,我应该做什么,即下面的代码应该打印在两页上,因为行的长度是 1400,打印文档长度是 1100,所以应该打印剩余的 300 行下一页

protected void ThePrintDocument_PrintPage (object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
Pen P1 = new Pen(Brushes.Violet, 5);
ev.Graphics.DrawLine(P1, new Point(0,0), new Point(500,1400));
}

最佳答案

这不是打印在 .NET 中的工作方式。它不会仅仅因为您在当前页面的坐标之外打印就创建新页面。 .NET 使用事件和事件参数来询问您的文档将包含多少页。然后它将调用事件以为每一页打印一页

参见 here举个例子。

编辑
好的,回复您的评论,我可以想到两种可能的解决方案:第一种解决方案涉及裁剪:将您的图形对象与页面的矩形相交,并仅打印它们共同的部分。如果裁剪区域外有部分,则将该部分作为新的图形对象重新裁剪打印在新的页面上。重复此操作,直到剩余的图形适合页面矩形。但是,我想不出一种轻松做到这一点的方法。

我的第二个想法是:

  1. 计算打印所有图形对象所需的页数,方法是将图形对象的边界矩形除以一页“可视矩形”的高度(如果此除法有余数,则将页数增加一页) ).
  2. 循环以下内容直到到达最后一页
    • 将所有图形对象打印到当前页面
    • 将所有 y 坐标减小页面的“视觉高度”(有效地将图形对象向上移动到“纸张边界”之外)

虽然为每一页打印整个图形对象列表的开销可能很高,但您将裁剪部分留给了打印机驱动程序/打印机本身。

关于c# - 基于 C# 中使用的坐标,使用 System.Drawing.Printing 在多个页面上打印图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198937/

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