根据C#中使用的坐标,使用System.Drawing.Printing在多页上打印图形

问题描述

|| 我想使用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使用一些事件和事件参数来询问您的文档将包含多少页。然后它将调用事件以为每个页面打印一个页面。 请参阅此处的示例。 编辑 好的,在回复您的评论时,我可以想到两个可能的解决方案:第一个解决方案将涉及裁剪:将图形对象与页面的矩形相交,并仅打印这两个对象的共同点。如果裁剪区域之外有零件,则将该零件作为新的图形对象,然后再次裁剪以在新页面上打印。重复此操作,直到其余图形适合页面矩形。但是,我想不出一种简单的方法。 我的第二个想法如下: 通过将图形对象的边界矩形除以一页的“可视矩形”的高度,计算出打印所有图形对象所需的页面数(如果此页面中有余数,则将页面数增加一页)师)。 循环播放以下内容,直到到达最后一页 将所有图形对象打印到当前页面 将所有y坐标减少一页的“视觉高度”(有效地将图形对象向上移动到“纸张边界”之外) 尽管为每页打印整个图形对象列表的开销可能很高,但您会将剪切部分留给了打印机驱动程序/打印机本身。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...