简体   繁体   English

在PrintPreviewControl上显示PrintDocument的所有页面

[英]Show all pages of PrintDocument on PrintPreviewControl

I'm having trouble creating multiple pages in a PrintDocument and displaying them within a PrintPreviewControl. 我在PrintDocument中创建多个页面并在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. 我最终将使用GDI绘制几页的内容,但无法像预期的那样工作。

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. 想法是创建3个单独的页面,并在PrintPreview控件中显示它们。 What am I missing? 我想念什么?

Your code snippet got mangled exactly at the critical point, where you assign e.HasMorePages. 您的代码段恰好在您分配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. 代码中存在一个明显的问题:您需要实现BeginPrint事件处理程序,以将页面计数器重置为0。

我不确定默认情况下如何显示所有页面,但是可以通过设置“ 属性”窗口的“ 布局”部分中的“ Columns属性和/或“ 属性”窗口中的“ Rows属性来在PrintPreviewControl显示多个页面。 行为部分,其值大于1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM