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