Printing HasMorePages not working c#

心已入冬 提交于 2019-12-24 00:14:13

问题


Alright so I have been looking around (on SO and Google) to see if a question could solve my error but apparently not, so here it goes:

I am trying to print something that will sometimes have more than one page and I am checking how many pages there is to print. After checking all this logic, I use the HasMorePages property to set if there is another page or not. This is my code:

int currentpage = 0;
int pagesleft = 0;
private void doc_PrintPage(object sender, PrintPageEventArgs ev)
{
    ev.Graphics.Clear(Color.White);
    int numofpages = (int)Math.Ceiling((double)(numofwords / 29.0));
    numofpages = (int)Math.Ceiling((double)(numofwords / 29.0));
    currentpage = currentpage + 1;
    if(currentpage == 1)
    {
        pagesleft = numofpages;
    }
    if (numofwordsleft >= 29)
    {
        currentwords = 29;
    }
    else
    {
        currentwords = numofwordsleft;
    }
    Font f19 = new Font("Arial", (float)19);
    Font f9 = new Font("Arial", (float)9);            

    ev.Graphics.DrawString("Pages: "+numofpages.ToString()+"         Words: "+numofwords.ToString()+"     "+currentwords.ToString(), f19, Brushes.Red, 200, 300);
    ev.Graphics.DrawString("Words Left: "+numofwordsleft, f19, Brushes.Green, 200, 500);

    ev.Graphics.DrawString("Pages: "+numofpages.ToString()+"       Current Page: "+currentpage.ToString(), f19, Brushes.Blue, 200, 700);

    numofwordsleft = numofwordsleft - currentwords;
    pagesleft = pagesleft - 1;

    //currentpage++;
    //if (currentpage != numofpages && currentpage < numofpages && pagesleft > 0 && pagesleft != 0)
    if(currentpage < numofpages)
    {
        ev.HasMorePages = true;
    }
    else 
    {
        ev.HasMorePages = false;
    }
}

Now everything works like it should, but when I try to print more than one page it prints 2 pages instead. I did some debugging and found this:

So HasMorePages is equal to false so it should only print 1 page. I pressed continue debugging and it hit the breakpoint at the end again. Since I clear the page at the beginning I was only seeing the second page (in a printpreview) but the printpreview said that there was only 1 page and the only page that was being displayed was the second one.

Here is the printpreview:

For the blue text I am doing this:

ev.Graphics.DrawString("Pages: "+numofpages.ToString()+"    Current Page: "+currenpage.ToString(), ...);

And the print preview clearly says that it is on Page 1.

Sorry for the long post but I need help. Please post here if you can help me/point me in the right direction to solving this.

Thanks!

Edit: So when I try to print 2 pages, using:

int numofpages = Math.Ceiling(39.0 / 29.0);

It prints 3 pages but all on the same page as shown in PrintPreview unless that isn't working properly.


回答1:


try this

    int currentpage = 0;
    int pagesleft = 0;
    int numofwordsleft = 0;
    int currentwords = 0;//For testing
    int numofwords = 120; // For testing
    private void doc_PrintPage(object sender, PrintPageEventArgs ev)
    {
        ev.Graphics.Clear(Color.White);
        int numofpages = (int)Math.Ceiling((double)(numofwords / 29.0));
        numofpages = (int)Math.Ceiling((double)(numofwords / 29.0));
        currentpage = currentpage + 1;
        if (currentpage == 1)
        {
            pagesleft = numofpages;
        }
        if (numofwordsleft >= 29)
        {
            currentwords = 29;
        }
        else
        {
            currentwords = numofwordsleft;
        }
        Font f19 = new Font("Arial", (float)19);
        Font f9 = new Font("Arial", (float)9);

        ev.Graphics.DrawString("Pages: " + numofpages.ToString() + "         Words: " + numofwords.ToString() + "     " + currentwords.ToString(), f19, Brushes.Red, 200, 300);
        ev.Graphics.DrawString("Words Left: " + numofwordsleft, f19, Brushes.Green, 200, 500);

        ev.Graphics.DrawString("Pages: " + numofpages.ToString() + "       Current Page: " + currentpage.ToString(), f19, Brushes.Blue, 200, 700);

        numofwordsleft = numofwordsleft - currentwords;
        pagesleft = pagesleft - 1;

        //currentpage++;
        //if (currentpage != numofpages && currentpage < numofpages && pagesleft > 0 && pagesleft != 0)
        if (currentpage < numofpages)
        {
            ev.HasMorePages = true;
        }
        else
        {
            ev.HasMorePages = false;
        }
    }

hope this helps. This is the preview i am getting after changing number of words.




回答2:


Try with the following:

int currentpage = 0;
private void doc_PrintPage(object sender, PrintPageEventArgs ev)
{
    ev.Graphics.Clear(Color.White);
    int numofpages = (int)Math.Ceiling((double)(numofwords / 29.0));
    currentpage++;
    // Print some graphics onto ev.Graphics
    ev.HasMorePages = (currentpage < numofpages)
}

Do not forget to include currentpage=0; before starting a new print!

UPDATE:

Your print starts by a button click or a menuitem click or whatever. Assuming that you use a button, what is expected (as minimum) is something like this:

private void cmdStartPrint_Click(object sender, System.EventArgs e)
{
    currentpage = 0;
    PrintDocument pd = new PrintDocument();
    pd.PrintPage  += new PrintPageEventHandler(this.doc_PrintPage);
    pd.Print();
}

And a tip: dispose what is disposable (including pd)



来源:https://stackoverflow.com/questions/11067746/printing-hasmorepages-not-working-c-sharp

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