打印HasMorePages不起作用c#

时间:2012-06-16 23:08:50

标签: c# printing

好吧所以我一直在四处寻找(在SO和谷歌上),看看问题是否可以解决我的错误,但显然不是,所以在这里:

我正在尝试打印有时会有多个页面的内容,而我正在检查要打印的页数。检查完所有这些逻辑后,我使用HasMorePages属性设置是否有其他页面。这是我的代码:

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页。我做了一些调试,发现了这个:

enter image description here

所以HasMorePages等于false所以它应该只打印1页。我按下继续调试,它再次点击断点。由于我在开始时清除页面,我只看到第二页(在printpreview中)但printpreview表示只有1页,而唯一显示的页面是第二页

以下是printpreview

enter image description here

对于蓝色文字,我这样做:

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

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

很抱歉很长的帖子,但我需要帮助。如果你能帮我/指出正确的方向来解决这个问题,请在这里发帖。

谢谢!

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

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

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

2 个答案:

答案 0 :(得分:1)

尝试以下方法:

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

不要忘记包含currentpage = 0;在开始新的印刷之前!

更新:

您的打印通过单击按钮或菜单项单击或其他任何内容开始。假设您使用了一个按钮,那么预期的内容(最小值)是这样的:

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

并提示:处理一次性用品(包括pd)

答案 1 :(得分:1)

试试这个

    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;
        }
    }
希望这会有所帮助。 这是我在更改单词数后得到的预览。 Print Preview Screen Shot