Winforms打印多页文档

时间:2016-04-21 04:16:23

标签: c# .net winforms printdocument

我试图在PrintDocumen的Print页面事件中将每页的记录数限制为20:

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
        }

我调用方法SetInvoiceData(e.Graphics, e),其代码循环通过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;
                }
            }

但是,永远不会创建新页面,它会将记录绘制到当前页面的末尾,忽略页面的下边距并停止。当我通过调试器运行它时,我可以看到它调用e.HasMorePages = true;语句,但正如我所说,没有新的页面被添加到文档中。请指教。

1 个答案:

答案 0 :(得分:0)

查看this页面了解工作示例。在那里你可以看到你需要设置HasMore页面:

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