Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}


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



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!)..

EDIT:

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

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 Edit: It is great now. Thank you Nick..
Posted
Updated 2-Sep-17 4:31am
v5

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 ==

C#
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!



== MODIFIED ==

This is just one example. There are many ways you could implement this.

Nick
 
Share this answer
 
v2
Comments
Member 13080248 23-Mar-17 14:23pm    
Thank you, Nick. You are a gentleman and a scholar.

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

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900