How to jump to the next page in a PrintDocument?

匆匆过客 提交于 2019-12-23 09:57:59

问题


I have an application that prints how many bar codes you want, but if the amount of bar codes is bigger than the size of the PrintDocument it doesn't jump to the next page.

I'd like to know how can I add more pages or write in the next page of a PrintDocument.

I'm using a PrintPreview to display the PrintDocument in this Windows Form.


回答1:


If you hookup the OnPrintPage event you can tell the PrintDocument if it needs to add another page on the 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;
}


来源:https://stackoverflow.com/questions/18814493/how-to-jump-to-the-next-page-in-a-printdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!