简体   繁体   English

多个页面的打印预览不起作用

[英]Print Preview of multiple pages not working

I've seen the numerous posts about this. 我看过很多关于这个的文章。 I believe I'm following them, but still having a problem. 我相信我一直在关注他们,但仍然有问题。

I'm doing this in C# and I'm running Windows 8.1. 我正在C#中执行此操作,并且正在运行Windows 8.1。

I am making a multi-page print-out of the contents of an XML file. 我正在对XML文件的内容进行多页打印。 I'm not just looping through all of the elements, I'm doing my own formatting of them. 我不仅在遍历所有元素,还在对它们自己进行格式化。 There are enough elements that I'll end up printing several pages, but I'm getting stuck on getting page 2 content to show up on page 2. Here's what I'm doing. 有足够的元素可以结束多页的打印,但是我陷入了使第2页的内容显示在第2页的困境。这就是我正在做的事情。

int pagePrinting;

private void butPrint_Click(object sender, EventArgs e)
{
    pagePrinting = 1;
    printDocument1.PrintPage += this.printDocument1_PrintPage;
    printPreviewDialog1.PrintPreviewControl.Document = printDocument1;
    printPreviewDialog1.Show();
    ((Form)printPreviewDialog1).WindowState = FormWindowState.Maximized;
}



private void printDocument1_PrintPage(System.Object sender, PrintPageEventArgs e)
{
    Point pnt = new Point(0, 0);
    switch (pagePrinting)
    {
        case 1:
            e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            e.Graphics.Clear(Color.White);
            printDocument1.DefaultPageSettings.Landscape = false;
            pnt.X = 300;
            e.Graphics.DrawString("Page 1 Header", fntArialHeader, Brushes.Black, pnt);
            // more printing on page 1 ...    

            pagePrinting++;
            e.HasMorePages = true;
            break;

        case 2:
            e.Graphics.DrawString("Page 2 Header", fntArialHeader, Brushes.Green, pnt);
            // More printing on page 2 ...

            e.HasMorePages = false;
            break;
    }
}

I believe that at the end of printing page 1, I am setting e.HasMorePages = true, then exiting and returning to print page 2. When I view this in Print Preview, I get both headers on page 1, which is the only page available. 我相信在打印页面1的末尾,我将设置e.HasMorePages = true,然后退出并返回到打印页面2。当我在“打印预览”中查看该页面时,我在页面1上获得了两个页眉,这是唯一的页面可用。 When I then hit the Print button on the Print Preview dialog, I get page 2 printed only. 然后,当我点击“打印预览”对话框上的“打印”按钮时,仅打印第二页。

I wouldn't constantly add the PrintPage event handler every time you click the print button: 每次您单击打印按钮时,我都不会不断添加PrintPage事件处理程序:

So comment out this line, or move it to the form's constructor so that it's only wired up once: 因此,注释掉这一行,或将其移至表单的构造函数,以便仅将其连线一次:

printDocument1.PrintPage += this.printDocument1_PrintPage;

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

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