Author Topic: How do I print multiple fields over multiple pages  (Read 1971 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
How do I print multiple fields over multiple pages
« on: April 05, 2009, 12:10:42 AM »
I don't have a problem with printing except that whenever my document grows beyond one page, the bottom margin on page 1 is printed in and each subsequent page has printing in the top margin and the bottom margin

If I could simply print the entire document with one call to DrawString while contraining to the printing bounds I wouldn't be having this problem, but the problem is more complex.

I have a minimum of 108 different fixed size fields that must be printed,(i.e they are NN characters long), plus 7 dynamic fields that the length is unknown.

I'd post the code, but it is pretty huge considering I have to set the XY corrdinates, font, justification, and pen for each item, plus each item has a box drawn around it. In the end, the result looks similar to a spreadsheet.

For a single page, everything works great, it is just when the document fields grow so the document will no longer fit on one page. Surely there is a better way than checking the Y coordinate of each item and cutting off the printing if it exceeds the allotted space, especially considering that many of the fields are in different controls and I am looping through each control to get the items to print. If the page height is exceeded within the loop, this would seem to cause problems because the fields wouldn't be printed completley before the loop would start again.

I'll entertain any suggestions

I'll attach a PDF so you can understand what I am talking about.

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Spike Wilbury

  • Guest
Re: How do I print multiple fields over multiple pages
« Reply #1 on: April 05, 2009, 01:09:46 PM »
There is class here that might be useful:

http://www.codeproject.com/KB/printing/multipadprintdocument.aspx

Here is one function, from one of the samples I have, just in case:

Code: [Select]
    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();
   
    }

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How do I print multiple fields over multiple pages
« Reply #2 on: April 09, 2009, 02:34:35 PM »
Unfortunately, that code is not very useful in my situation.

The problem I face is having multiple fields per page that are dynamic in size. Field 1 might be 1 line, or it might be 1000 lines, so I have to check each field for every page, because it is not possible to know exactly where printing stops and starts from document to document.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie