views:

414

answers:

1

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?

A: 

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.

Hans Passant
I've added such an EventHandler, but it still only displays the first page in the PrintPreviewControl.The "PrintPage" function is executed three times, but the only page that is displayed is the one containing the "page1" string.I can't seem to figure out how to add new pages.
Jesse
I dunno, you didn't fix your snippet. Just to be sure: you are using the page number selector in the preview dialog correctly? Upper right side of the toolbar.
Hans Passant
That definitely worked, thanks; however, is there a way to display all of the pages together? Is there a way to display all three pages successively in the PrintPreviewControl?
Jesse
Use the toolbar. You can display up to 6 pages at the same time.
Hans Passant
I'm not trying to be dense, but I don't know of what toolbar you're referring to. I assume it to be some print-related toolbar, but I'm unfamiliar as to where it is.
Jesse
Oops, I was talking about PrintPreviewDialog. Well, recommended.
Hans Passant
Ah, ok, thanks. I appreciate your help.
Jesse