如何在C#中打印更多页面(printdocument) [英] How to print more pages in C# (printdocument)

查看:115
本文介绍了如何在C#中打印更多页面(printdocument)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这就是事情。我想在更多的页面上打印一些文本,用户在文本框中插入的文本(开玩笑以获得图像)..我将给你一个简单的例子说明我想要做的事情:



点击一个按钮我想打电话给打印预览对话框(这是怎么回事):



Ok, so here is the thing. I wanna print some text on more pages, text that would be inserted by the user in a textbox (jest to have an image).. I ll give you a simple example of what i am trying to do:

on a click of a button i wanna call a print preview dialog (here is how):

private void button2_Click(object sender, EventArgs e)
{
       printPreviewDialog1.Document = printDocument1;
       printPreviewDialog1.ShowDialog();
}





在printdocument中这之后我这样做:(不是真正的代码,开玩笑的例子)





After this in printdocument i am doing this: (not the real code, jest a little example)

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
       Font myFont = new Font("m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point);
       int y = e.MarginBounds.Top;
       for (int i=0; i<70; i++)
       {
              e.Graphics.DrawString("TEST", myFont, Brushes.Black, new PointF(e.MarginBounds.Left, y));
              y += 35;
       }
}







所以,像这样它不会写全部70行,所以它需要打开一个新的页面..但是如何做到这一点..我尝试了e.HasMorePages,但没有成功。所以,如果有人知道如何解决这个问题,请告诉我..

(解决这个简单的例子对我非常有帮助!)..



编辑:



感谢您的回复。但我仍然无法解决这个问题..我已经看过了,但这对我来说有点复杂(没有提到我是初学者)

例如,在那个项目中我不知道什么是/做StreamReader streamToPrint,所以它开玩笑让我感到困惑..

这是我试过的(我认为这是正确的):




So, like this it will not write all 70 lines, so it needs to open a new page.. But HOW to do it.. I have try with "e.HasMorePages", but no success with it. So please if someone knows how to solve this problem, let me know..
(solving this simple example would help me very very much!)..



Thanks for the response. But i still cant solve this.. I have already seen that, but it's little complicated to me (didn't mention that i am beginner)
Like for example, in that project i don't know what is/do "StreamReader streamToPrint", so it jest confuses me..
Here is what i tried (what i thought that would be correct):

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font myFont = new Font("m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point);            
            int y = e.MarginBounds.Top;
            for (int i = 0; i &lt; 70; i++)
            {                
                if (y &gt;= e.MarginBounds.Bottom)
                {
                    e.HasMorePages = true;
                    y = e.MarginBounds.Top;
                }
                else
                {
                    e.HasMorePages = false;
                    e.Graphics.DrawString("TEST", myFont, Brushes.Black, new PointF(e.MarginBounds.Left, y));
                    y += 25;
                }
            }
        }



但这不起作用..

现在如果你(任何人都可以给我写下代码,其中70个单词TEST将被写入,一个在另一个下面,在几个页面上,因为它需要..或任意数量的单词,我需要编译器知道应该打开多少个新页面,并在想要的页面写下想要的文字..

我希望你能理解我想知道的,你会帮助我..

谢谢你转发..



最后编辑:现在很棒。谢谢Nick ..


But this doesn't work..
Now if you(anyone) could write me the code where 70 lines of the word TEST would be written, one under another, on several pages as it is needed.. or any number of words, i jest need the compiler to know how many new pages should open, and write the wanted text in the wanted page..
I hope you could understand me what i wanna know, and that you ll help me..
Thanks you Forward..

Last It is great now. Thank you Nick..

推荐答案

我认为你遗漏的一点是 PrintPage 被提出一次每页。如果在完成并退出处理程序时设置 e.HasMorePages = true; ,则会再次调用它以打印下一页。这将继续,直到您设置 e.HasMorePages = false; 表示您已完成所有页面。



这会给你一个想法:



== MODIFIED ==



I think the point you're missing is that PrintPage is raised once for each page. If you set e.HasMorePages = true; when you finish and exit the handler, then it will be called again to print the next page. This will continue until you set e.HasMorePages = false; which indicates that you have finished all your pages.

This will give you the idea:

== MODIFIED ==

private int _Line = 0;

private void button2_Click(object sender, EventArgs e)
{
    printPreviewDialog1.Document = printDocument1;

    _Line = 0; // initialize printing
    printPreviewDialog1.ShowDialog();
}

void printDocument1_PrintPage( object sender, PrintPageEventArgs e )
{
    Font myFont = new Font( "m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point );

    float lineHeight = myFont.GetHeight( e.Graphics ) + 4;

    float yLineTop = e.MarginBounds.Top;

    for ( ; _Line < 70 ; _Line++ )
    {
        if ( yLineTop + lineHeight > e.MarginBounds.Bottom )
        {
            e.HasMorePages = true;
            return;
        }

        e.Graphics.DrawString( "TEST: " + _Line, myFont, Brushes.Black,
            new PointF( e.MarginBounds.Left, yLineTop ) );

        yLineTop += lineHeight;
    }

    e.HasMorePages = false;
}



对于以前的(损坏的)版本很抱歉。我实际上跑了这个!


Sorry for the previous ( broken ) version. I actually ran this one!


如果你还没有完成并需要另一个页面,那么设置:

If you haven't finished and need another page, then set:
PrintPageEventArgs.HasMorePages = true;


$ b您的 PrintPage 处理程序中的$ b。然后使用新的 PrintPageEventArgs.MarginBounds 再次调用您的处理程序。用这个来计算要打印的页面。



MSDN [ ^ ]。



Nick


in your PrintPage handler. Then your handler will be called again with new PrintPageEventArgs.MarginBounds. Use this to calculate which page to print.

There is an ( uncharacteristically ) good example on MSDN[^].

Nick

这篇关于如何在C#中打印更多页面(printdocument)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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