PrintDocument HasMorePages 不起作用 [英] PrintDocument HasMorePages don't work

查看:53
本文介绍了PrintDocument HasMorePages 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印数据网格中的数据.该代码适用于第一页,但注释行效果不佳并且不会移动到下一页.谁能帮忙解决这个问题?

I want to print the data from data grid. The code works well for the first page, but the commented lines do not works well and don't move to next page. Can anyone help fix this?

private void DrawFactorA4(object sender, PrintPageEventArgs ev)
    {
     for (int j = 0; j < GrdRDocument.Rows.Count; j++)
        {
            i += 2;
            //draw data grid
            s++;
            if(s == 10)
            {    
               //ev.HasMorePages = true;  //this line doesn't work
               s = 0;
               i = 0;                 
            }
            else
            {
               ev.HasMorePages = false;                    
            }
        }
    }

_

private void BtnPrint_Click(object sender, EventArgs e)
    {
        printFont = new Font("Arial", 12);

        IEnumerable<PaperSize> paperSizes = 

        pd.PrinterSettings.PaperSizes.Cast<PaperSize>();

        sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4);

        pd.DefaultPageSettings.Landscape = true;

        pd.DefaultPageSettings.PaperSize = sizeA4;

        pd.PrintPage += new PrintPageEventHandler(this.DrawFactorA4);

        printPreviewDialog.Document = pd;

        printPreviewDialog.ShowDialog();
    }

推荐答案

停下来阅读你所拥有的:

Stop and read what you have:

printFont = new Font("Arial", 12);

字体是非托管资源;在这里,您正在实例化一个并且从不处理它.在这种特殊情况下,这可能是无害的,但这是一个不好养成的习惯.

Fonts are unmanaged resources; here you're instantating one and never disposing it. Maybe that's harmless in this particular situation, but this is a bad habit to get in to.

pd.PrintPage += new PrintPageEventHandler(this.DrawFactorA4);

DrawFactorA4 将在文档中的每个页面上调用.DrawFactorA4 内部:

DrawFactorA4 is going to be called for every page in your document. Inside DrawFactorA4:

for (int j = 0; j < GrdRDocument.Rows.Count; j++)

您遍历 GrdRDocument 中的每个 Row,无论行数或页面大小如何.那是错的;您必须在页面填满后停止.顺便说一句,我希望 GrdRDocument 是不可变数据的本地副本,并且您不会将 UI 控件传递给打印线程.

You iterate through every Row in GrdRDocument, regardless the number of rows or the size of your page. That is wrong; you have to stop after the page is filled. By the way, I hope GrdRDocument is a local copy of immutable data and you're not passing UI controls to the printing thread.

s++;
if(s == 10)
{    
    //ev.HasMorePages = true;  //this line doesn't work
    s = 0;

您的注释行会正常工作.问题是你设置了 ev.HasMorePages = true 然后忽略它;你设置 s = 0 并不断迭代;下一次迭代 s!=10 所以你:

Your commented line would work fine. The problem is you set ev.HasMorePages = true and then ignore it; you set s = 0 and keep iterating; next iteration s!=10 so you:

    ev.HasMorePages = false;

阅读PrintDocument 文档;它有一个打印不止一页的例子.您应该创建一个类来存储所有非托管资源和页面状态.使其成为 IDisposable 以便它们得到处理.仅遍历行或您想在每页上打印的任何内容.类似的东西:

Read the PrintDocument docs; it has an example of printing more than one page. You should create a class to store all the unmanaged resources and page state. Make it IDisposable so they get disposed. Iterate through only the rows or whatever you want to print on each page. Something like:

class PrintStuff : IDisposable
{
    readonly IEnumerable<Whatever> data;
    readonly PrintDocument pd;
    Font font;
    private int currentIndex;

    public PrintStuff(IEnumerable<Whatever> data)
    {
        this.data = data;

        pd = new PrintDocument();
        pd.BeginPrint += OnBeginPrint;
        pd.PrintPage += OnPrintPage;
        pd.EndPrint += OnEndPrint;
    }

    public void Print()
    {
        pd.Print();
    }

    public void Dispose()
    {
        pd.Dispose();
    }

    private void OnBeginPrint(object sender, PrintEventArgs args)
    {
        font = new Font(FontFamily.GenericSansSerif, 12F);
        currentIndex = 0;
    }

    private void OnEndPrint(object sender, PrintEventArgs args)
    {
        font.Dispose();
    }

    private void OnPrintPage(object sender, PrintPageEventArgs args)
    {
        var x = Convert.ToSingle(args.MarginBounds.Left);
        var y = Convert.ToSingle(args.MarginBounds.Top);
        var lineHeight = font.GetHeight(args.Graphics);
        while ((currentIndex < data.Count())
               && (y <= args.MarginBounds.Bottom))
        {
            args.Graphics.DrawWhatever(data.ElementAt(currentIndex), font, Brushes.Black, x, y);
            y += lineHeight;
            currentIndex++;
        }

        args.HasMorePages = currentIndex < data.Count();
    }
}

这篇关于PrintDocument HasMorePages 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆