如何在PrintDocument中跳转到下一页?

6
我有一个应用程序,可以打印任意数量的条形码,但如果条形码数量超出了PrintDocument的大小,它就不会跳到下一页。
我想知道如何添加更多页面,或者写在PrintDocument的下一页上。
我使用PrintPreview在这个Windows表单中显示PrintDocument。

1
在您的PrintPage事件处理程序中使用e.HasMorePages属性。这个MSDN文章中讲得很好:http://msdn.microsoft.com/en-us/library/cwbe712d.aspx - Hans Passant
1个回答

6
如果您挂钩OnPrintPage事件,可以告诉PrintDocument是否需要在PrintPageEventArguments上添加另一页。
IEnumerator items;

public void StartPrint()
{
   PrintDocument pd = new PrintDocument();
   pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
   items = GetEnumerator();
   if (items.MoveNext())
   {
       pd.Print();
   }
}

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    const int neededHeight = 200;
    int line =0;
    // this will be called multiple times, so keep track where you are...
    // do your drawings, calculating how much space you have left on one page
    bool more = true;
    do
    {
        // draw your bars for item, handle multilple columns if needed
        var item = items.Current;
        line++;
        // in the ev.MarginBouds the width and height of this page is available
        // you use that to see if a next row will fit
        if ((line * neededHeight) < ev.MarginBounds.Height )
        {
            break;
        }
        more = items.MoveNext();
    } while (more);
    // stop if there are no more items in your Iterator
    ev.HasMorePages = more;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接