Ken Barrett

Ken Barrett

  • NA
  • 123
  • 0

printing multiple pages

Aug 14 2009 7:34 PM
Ok, this ought to be a no brainer.. but apparently it's ME that's a no brainer. 
[rant]
OK, I'm a BIT frustrated here.. I've programmed in PERL, C++, C, and various UNIX shell languages for years.  Printing was something that was the easiest thing to do!  This is not rocket science. I'm not building a rocket to fly to MARS.. I just want to print my report.
[/rant]

I am creating a report that may or may not be of multiple pages.  For the life of me I cannot get it to print correctly.

Here's the code. (the report is coming to it from a rich text box)

    // =========================================================================
    // Menu strip File -> Print click event
    // send the data directly to the printer
    private void genPrint(object sender, EventArgs e) { // =====================
      PrintDocument pd = new PrintDocument();
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage) ; // function below
      printDialog1.Document = pd ;
      DialogResult result = printDialog1.ShowDialog() ;
      if (result==DialogResult.OK)  pd.Print() ;
    } // =======================================================================

    // =========================================================================
    // function used by PrintPageEventHandler
    private void pd_PrintPage(object sender, PrintPageEventArgs ev) { // =======
      string[] pBody = rtbReport.Text.Split('\n') ;
      Font printFont = new Font("Courier New", 10) ;
      float maxLines = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;
      float top  = ev.MarginBounds.Top ;
      float left = ev.MarginBounds.Left ;
           
      for(int i=0 ; i < pBody.Length ; i++) { // Print each line of the file
        ev.HasMorePages = true ;
        for(int num=0 ; num < maxLines && i < pBody.Length ; num++) {
          float yPos = top + (num * printFont.GetHeight(ev.Graphics)) ;
          ev.Graphics.DrawString(pBody[i++],printFont,Brushes.Black,left,yPos) ;
        }
        if (i >= pBody.Length)      // this shouldn't be necessary, but if I leave
          ev.HasMorePages = false ; // it out I get in infinite number of pages
                                    // IF I leave it in, I just get the first page
      }                             // of my document
    } // =======================================================================


I could really use some help here.  I can't find any documentation (that works) on line or in any of my books.  WHAT the heck is going wrong here.  I get the first page only of my report.  If I take the ev.HasMorePages = false part out, then I get a never ending number of pages in my report.

PLEASE HELP!!

This is driving me absolutely NUTS.

Edited to add :
I just tried using the code at :
http://www.c-sharpcorner.com/UploadFile/mgold/PritinginCSharp11222005040630AM/PritinginCSharp.aspx
And got the same exact result.

HELP!!

Answers (4)