Click here to Skip to main content
15,889,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are tons of solutions about how to handle printing multiple pages when there is not enough room to print on a single page. However, I can not find a solution where you can print a multiple page report with print preview. It's not that my information doesn't fit, it fits perfectly as I use the same code to print a single page. Just without the For-Next Loop.

Can someone show me how to use Print Preview with a multiple page report? Each page is exactly the same, just has different information on it.

What I have tried:

Here is my code:

void printButton_Click(object sender, EventArgs e)
        {
            printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(500, 500, 500, 500);
            printDialogPreview.Document = printDocument1;
            printDialogPreview.Document.DocumentName = MainForm._fileName;
            if (printDialogPreview.ShowDialog() == DialogResult.OK)
            {
                //printDocument1.PrinterSettings = printDialogPreview.PrinterSettings;
                printDocument1.Print();
            }
        }

        private void CapturePrintPanel(Control pnl)
        {
            Graphics myGraphics = pnl.CreateGraphics();
            memoryImage = new Bitmap(pnl.Width, pnl.Height);
            Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
            pnl.DrawToBitmap(memoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
        }

        private void printDocument1_PrintPage(System.Object sender,
               System.Drawing.Printing.PrintPageEventArgs e)
        {
            for (int i = 0; i < Readings.Count; i++)
            {
                pP = new PrintPreview(Readings[i]);
                pP.Show();
                CapturePrintPanel(pP.Controls["printPanel"]);
                e.Graphics.DrawImage(memoryImage, 0, 0);
                pP.Close();
                pP.Dispose();
                if((i + 1) < Readings.Count)
                {
                    e.HasMorePages = i < Readings.Count;
                }
            }
            e.HasMorePages = false;
        }
Posted
Updated 21-Jul-19 4:13am

1 solution

When you use a PrintDocument, it raises a PrintPage event and passes a PrintPageEventArgs which provides information on how to print - including the Graphics object you should print on. When you use PrintPreview the context is for the display, for a printer it's the actual "print surface" that will be rendered on the paper (or PDF, or whatever).
One of the properties of the EventArgs is the HasMorePages property which if true will cause another page to be printed after the first is complete.

Each time PrintPage is called, you print just that page's worth of info on it, and save information at the class level for the next page. So you would use the same for loop, but instead of starting at "row zero" and printing to the end, you print row 0 - 24 on page one, set the HasMorePages property to true, then print rows 25 - 49 on page two - then set HasMorePages to false if there are no more pages to show.

Give it a try just printing 3 pages with a big "1", "2", or "3" in the middle - you'll get the idea pretty soon!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900