Show all pages of PrintDocument on PrintPreviewControl
-
18-09-2019 - |
Question
I'm having trouble creating multiple pages in a PrintDocument and displaying them within a PrintPreviewControl. I can create a single page easily enough, but stringing together multiple pages is eluding me.
I'm going to eventually draw several pages of stuff using GDI, but I can't get something like this to work as expected.
private PrintDocument doc = new PrintDocument();
private string[] printMe = new string[]{ "page1", "page2", "page3" );
private int pageCount = 0;
private void FormLoad(object sender, EventArgs e)
{
doc.PrintPage += new PrintPageEventHandler(PrintPage);
PrintPreviewControl.Document = doc;
}
private void doc_BeginPrint(object sender, PrintEventArgs e){ pageCount = 0; }
private void PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString(drawMe[pageCount++], "Lucida Console", Brushes.Black, new Point(20,20));
e.HasMorePages = (pageCount printMe.Length );
}
The idea being that 3 separate pages are created, and displayed within the PrintPreview control. What am I missing?
Solution
Your code snippet got mangled exactly at the critical point, where you assign e.HasMorePages. There's one glaring problem in your code: you need to implement a BeginPrint event handler to reset the page counter back to 0.
OTHER TIPS
I'm not sure how to show all pages by default, but you can show more than one page in the PrintPreviewControl
by setting the Columns
property, found in the Layout section of the Properties window, and/or the Rows
property, found in the Behavior section, to a value higher than 1.