简体   繁体   中英

Printing graphics on multiple pages using System.Drawing.Printing, based on the used coordinates in C#

Using System.Drawing.Printing, I want to print a set of lines on print document. But the problem is it prints every line on the very first page, what ever the coordinates are, also if I draw an image, It prints that on a single page no matter how big it is.

Following is what I have done for printing text on multiple pages:

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();

}

What I should do in order to print line on multiple pages, ie the following code should print on two pages as the length of the line is 1400 and print doc length is 1100, so remaining 300 of the line should be printed on the next page

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));
}

This is not how printing works in .NET. It does not create a new page just because you print outside the coordinates of the current page. There are events and event arguments which .NET uses to ask from you how many pages your document will contain. It will then invoke the event for printing a page for every page .

See here for an example.

EDIT
Ok, replying to your comment, I can think of two possible solutions: The first solution would involve clipping: intersect your graphic objects with the page's rectangle and print only what's common to both of them. If there were parts outside the clipping region, take that part as the new graphic objects and clip again to print on new page. Repeat this until the remaining graphics fit the page rectangle. However, I can't think of a way of doing that easily.

My second idea is as follows:

  1. Calculate how many pages you would need to print all graphic objects by dividing the bounding rectangle of the graphic objects by the height of the "visual rectangle" of one page (increase number of pages by one if there's a remainder in this division).
  2. Loop the following until reaching the last page
    • Print all graphic objects to the current page
    • Decrease all y-coordinates by the "visual height" of a page (effectively moving the graphic objects upwards outside the "bounds of the paper")

While the overhead of printing the entire list of graphic objects for every page may be high, you're leaving the clipping part to the printer driver/the printer itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM