title: How do I print multiple pages from WinForms?
body: <p>Although there are some tutorials on web, I'm still lost on why this doesn't print multiple pages correctly.  What am I doing wrong?</p>
  
  <pre><code>public static void printTest()
  {
     PrintDialog printDialog1 = new PrintDialog();
     PrintDocument printDocument1 = new PrintDocument();
  
     printDialog1.Document = printDocument1;
     printDocument1.PrintPage += 
         new PrintPageEventHandler(printDocument1_PrintPage);
  
     DialogResult result = printDialog1.ShowDialog();
     if (result == DialogResult.OK)
     {
         printDocument1.Print();
     }       
  }
  
  static void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  {
     Graphics graphic = e.Graphics;
     SolidBrush brush = new SolidBrush(Color.Black);
  
     Font font = new Font("Courier New", 12);
  
     e.PageSettings.PaperSize = new PaperSize("A4", 850, 1100);
  
     float pageWidth = e.PageSettings.PrintableArea.Width;
     float pageHeight = e.PageSettings.PrintableArea.Height;
  
     float fontHeight = font.GetHeight();
     int startX = 40;
     int startY = 30;
     int offsetY = 40;
  
     for (int i = 0; i &lt; 100; i++)
     {             
         graphic.DrawString("Line: " + i, font, brush, startX, startY + offsetY);
         offsetY += (int)fontHeight;
  
         if (offsetY &gt;= pageHeight)
         {
             e.HasMorePages = true;
             offsetY = 0;
         }
         else {
             e.HasMorePages = false;
         }
     }
  }
  </code></pre>
  
  <p>You can find an example of this code's printed result here: <a href="https://docs.google.com/open?id=0B3AbXJ0xsx1TNmYwMmUyZGItZWI0OS00ZDM0LTk4MmUtODJjNDc1MWEzY2Ez" rel="nofollow">Printed Document</a></p>