打印 HasMorePages 不起作用 c# [英] Printing HasMorePages not working c#

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

问题描述

好吧,所以我一直在环顾四周(在 SO 和 Google 上)看看一个问题是否可以解决我的错误,但显然不能,所以它是这样的:

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:

我正在尝试打印有时会超过一页的内容,我正在检查有多少页要打印.检查完所有这些逻辑后,我使用 HasMorePages 属性来设置是否有另一个页面.这是我的代码:

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;
    }
}

现在一切正常,但是当我尝试打印不止一页时,它会打印 2 页.我做了一些调试,发现了这个:

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:

所以 HasMorePages 等于 false 所以它应该只打印 1 页.我按下了继续调试,它又在最后碰到了断点.由于我在开始时清除了页面,因此我只看到了第二页(在 printpreview 中),但是 printpreview 说只有 1 页,并且只有一页是显示的是第二个.

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.

这是printpreview:

对于蓝色文本,我正在这样做:

For the blue text I am doing this:

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

打印预览清楚地表明它在第 1 页上.

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.

谢谢!

所以当我尝试打印 2 页时,使用:

So when I try to print 2 pages, using:

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

它打印 3 页,但都在 PrintPreview 所示的同一页上,除非它不能正常工作.

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

推荐答案

试试这个

    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.

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

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