2016-06-30 60 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循環。我不用尋找身高,而是使用顯示的項目數量。 –

相關問題