2016-06-30 36 views
1

在我的Windows应用程序中,我在PrintPriview控件中打印了多个项目,但当项目大于25时,它不会打印下一页来打印其余项目。我知道我们需要使用e.Hasmorepages = true,但我无法弄清楚如何正确使用它。请帮忙。如何在c#中的PrintPreview控件中显示多个页面#

屏幕截图 - Click here to see screen shot

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    { 
     Bitmap bmp = Properties.Resources.logo2; 
     Image image = bmp; 
     e.Graphics.DrawImage(image, 0, 0, image.Width, image.Height); 

     e.Graphics.DrawString("Date: " + DateTime.Now.ToShortDateString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 160)); 
     e.Graphics.DrawString("Client Name: " + ClientNameTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 190)); 

     e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 235)); 
     e.Graphics.DrawString("Item Name", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(30, 255)); 
     e.Graphics.DrawString("Quantity", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(380, 255)); 
     e.Graphics.DrawString("Unit Price (£)", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(510, 255)); 
     e.Graphics.DrawString("Total Price (£)", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(660, 255)); 
     e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 270)); 

     int yPos = 295; 

     foreach (var i in shoppingCart) 
     { 
      e.Graphics.DrawString(i.ItemName, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(30, yPos)); 
      e.Graphics.DrawString(i.Quantity.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(400, yPos)); 
      e.Graphics.DrawString(i.UnitPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos)); 
      e.Graphics.DrawString(i.TotalPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(700, yPos)); 
      yPos += 30; 

     } 

     e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, yPos)); 

     e.Graphics.DrawString("Total Amount:  £" + TotalAmountTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 30)); 
     e.Graphics.DrawString("Sales Tax (16%): £" + SalesTaxTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 60)); 
     e.Graphics.DrawString("Total To Pay:  £" + TotalToPayTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 90)); 

     e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, yPos + 120)); 

    } 

在此先感谢。

问候,

回答

1

是的,你需要

一)观看y

二)设置HasMorePages为真时,它是在最大页面高度值

C) 离开打印页面事件

如果需要,它会再次调用,即当您打印或预览下页..

您还需要跟踪您在类级别的变量已打印车项目;用for循环代替foreach循环存储/使用该项目编号!在y大于适合页面的最大值的情况下,跳出循环,然后再从return开始!

同样设计用于重复标题,但你有正确的想法!最后,另一个班级变量应该跟踪页码

+0

谢谢,塔夫。我已将foreach更改为for循环。我不用寻找身高,而是使用显示的项目数量。 –

相关问题