Search code examples
c#.netwinformsprintdocument

Winforms print multi-page document


I am trying to limit number of records per page to 20, in my PrintDocumen's Print page event:

private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            leftMargin = (int)e.MarginBounds.Left;
            rightMargin = (int)e.MarginBounds.Right;
            topMargin = (int)e.MarginBounds.Top;
            bottomMargin = (int)e.MarginBounds.Bottom;
            InvoiceWidth = (int)e.MarginBounds.Width;
            InvoiceHeight = (int)e.MarginBounds.Height;

            SetInvoiceHeader(e.Graphics); // Draw Invoice Head
            SetOrderData(e.Graphics); // Draw Order Data
            SetInvoiceData(e.Graphics, e); // Draw Invoice Data
        }

I call method SetInvoiceData(e.Graphics, e) that has the following code looping through BindingSource bsOrderDetail:

foreach (DataRowView row in bsOrderDetail)
            {
                CurrentRecord++;

                string FieldValue = row[1].ToString();
                g.DrawString(FieldValue, InvSubTitleFont, BlackBrush, xProductID, CurrentY);
                FieldValue = row[2].ToString();
                if (FieldValue.Length > 20)
                    FieldValue = FieldValue.Remove(20, FieldValue.Length - 20);
                g.DrawString(FieldValue, InvSubTitleFont, BlackBrush, xProductName, CurrentY);

                FieldValue = row[3].ToString();
                g.DrawString(FieldValue, InvSubTitleFont, BlackBrush, xQuantity, CurrentY);
                FieldValue = row[4].ToString();
                g.DrawString(FieldValue, InvSubTitleFont, BlackBrush, xUOM, CurrentY);
                FieldValue = row[5].ToString();
                g.DrawString(FieldValue, InvSubTitleFont, BlackBrush, xUnitPrice, CurrentY);

                CurrentY = CurrentY + 24;

                if (CurrentRecord > RecordsPerPage)
                {
                    CurrentRecord = 1;
                    e.HasMorePages = true;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }

However new page is never created, it draws records to the end of the current page ignoring bottom margin of the page and stops. When I run it through debugger I can see it calling e.HasMorePages = true; statement, but as I said no new pages are being added to the document. Please advise.


Solution

  • Look at this page for a working example. There you can see you need to set HasMore pages:

    // Define the Printpage event of the printdocument
    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
      // Declare one variable for height measurement:
      float currentY = 10; 
    
      // This will print one heading in every page:
      e.Graphics.DrawString("PageHeader", DefaultFont, Brushes.Black, 10, currentY); 
      currentY += 15;
    
      while(totalnumber <= 50) // Check the number of items
      {
        // Print each item:
        e.Graphics.DrawString(totalnumber.ToString(),DefaultFont, Brushes.Black, 50,currentY); 
        currentY += 20; // Set a gap between every item
        totalnumber += 1;
    
        // Check whether  the number of item(per page) is more than 20 or not:
        if (itemperpage < 20) 
        {
          itemperpage += 1;
          // Set the HasMorePages property to false, so that no other page will not be added
          e.HasMorePages = false; 
        }
        else // If the number of item (per page) is more than 20 then add one page
        {
          itemperpage = 0;
          // e.HasMorePages raised the PrintPage event once per page
          e.HasMorePages = true;
          // It will call PrintPage event again
          return; 
        }
      }
    }