locked
How to show multiple pages in PrintPreview control in c# RRS feed

  • Question

  • Hi All,

    In my Windows application, I print multiple items in my PrintPriview control but when the items are more than 25, it doesn't go the next page to print the rest of the items. I know that we need to use e.Hasmorepages = true but I can't figure out how to use it correctly. Please help.

    Screen shot is below.

    The code is below.

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

    Thanks in advance.

    Regards,


    • Edited by eaak2004 Thursday, June 30, 2016 3:31 PM
    Thursday, June 30, 2016 3:28 PM

Answers

  • Thank Moonlight for reply, it doesn't solve my problem because the system has no way to end the printing. It is printing unlimited pages. So, after working for a few days, I come up with the solution shown below.

    // Add these variables at the class level
    private int numberOfItemsPerPage = 0;
    private int numberOfItemsPrintedSofar = 0;
    
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
                Image image = Properties.Resources.logo2;;
                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;
                int i = 0;
    
                for (i = numberOfItemsPrintedSofar; i < shoppingCart.Count; i++)
                {
                    numberOfItemsPerPage++;
    
                    if (numberOfItemsPerPage <= 25)
                    {
                        numberOfItemsPrintedSofar++;
                        
                        if (numberOfItemsPrintedSofar <= shoppingCart.Count)
                        {
                            e.Graphics.DrawString(shoppingCart[i].ItemName, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(30, yPos));
                            e.Graphics.DrawString(shoppingCart[i].Quantity.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(400, yPos));
                            e.Graphics.DrawString(shoppingCart[i].UnitPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos));
                            e.Graphics.DrawString(shoppingCart[i].TotalPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(700, yPos));
    
                            yPos += 30;
                        }
                        else
                        {
                            e.HasMorePages = false;
                        }
                    }
                    else
                    {
                        numberOfItemsPerPage = 0;
                        e.HasMorePages = true;
                        return;
                    }
                }
    
                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));
    
                // Reset Variables
                numberOfItemsPerPage = 0;
                numberOfItemsPrintedSofar = 0;
    }
    
    

    Thanks.


    • Marked as answer by eaak2004 Thursday, July 7, 2016 1:26 AM
    • Edited by eaak2004 Thursday, July 7, 2016 1:27 AM
    Thursday, July 7, 2016 1:26 AM

All replies

  • Hi eaak2004,

    You could use 'PrintPageEventArgs.HasMorePages' to enable multiple pages:

                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;
                    if (yPos - 295 > 30 * 25&& !e.HasMorePages)
                        e.HasMorePages = true;
                }

    Reference link:

    How to: Print a Multi-Page Text File in Windows Forms

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, July 1, 2016 2:49 AM
  • Thank Moonlight for reply, it doesn't solve my problem because the system has no way to end the printing. It is printing unlimited pages. So, after working for a few days, I come up with the solution shown below.

    // Add these variables at the class level
    private int numberOfItemsPerPage = 0;
    private int numberOfItemsPrintedSofar = 0;
    
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
                Image image = Properties.Resources.logo2;;
                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;
                int i = 0;
    
                for (i = numberOfItemsPrintedSofar; i < shoppingCart.Count; i++)
                {
                    numberOfItemsPerPage++;
    
                    if (numberOfItemsPerPage <= 25)
                    {
                        numberOfItemsPrintedSofar++;
                        
                        if (numberOfItemsPrintedSofar <= shoppingCart.Count)
                        {
                            e.Graphics.DrawString(shoppingCart[i].ItemName, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(30, yPos));
                            e.Graphics.DrawString(shoppingCart[i].Quantity.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(400, yPos));
                            e.Graphics.DrawString(shoppingCart[i].UnitPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos));
                            e.Graphics.DrawString(shoppingCart[i].TotalPrice.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(700, yPos));
    
                            yPos += 30;
                        }
                        else
                        {
                            e.HasMorePages = false;
                        }
                    }
                    else
                    {
                        numberOfItemsPerPage = 0;
                        e.HasMorePages = true;
                        return;
                    }
                }
    
                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));
    
                // Reset Variables
                numberOfItemsPerPage = 0;
                numberOfItemsPrintedSofar = 0;
    }
    
    

    Thanks.


    • Marked as answer by eaak2004 Thursday, July 7, 2016 1:26 AM
    • Edited by eaak2004 Thursday, July 7, 2016 1:27 AM
    Thursday, July 7, 2016 1:26 AM