Some problems understanding how to use e.HasMorePages and setting zoom

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1530 Views - Last Post: 02 April 2013 - 06:04 AM Rate Topic: -----

#1 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 215
  • Joined: 25-April 11

Some problems understanding how to use e.HasMorePages and setting zoom

Posted 21 March 2013 - 10:28 AM

Hello everybody

I have never realized any application with the possibility to print anything. So I am really new to print-methods and I have some basic questions.

If you look at my actual code here after, there ist the page set up with the information I would like to print, but I don't understand how to implement the e.HasMorePages. I have studied the examples shown on MSDN-Site, but I don't understand them. :helpsmilie:
At the bottom of my code you can see a commented test I tryed, but obviously it doesn't work! :withstupid:
Can someone point me in the right direction, and perhaps explain why I should do whatever you will tell me to do?

void PDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            FontFamily fontFamily = new FontFamily("Arial");
            Font fBoldU = new Font(fontFamily, 15, FontStyle.Bold | FontStyle.Underline, GraphicsUnit.Pixel);
            Font fNorm = new Font(fontFamily, 13, FontStyle.Regular, GraphicsUnit.Pixel);
            RectangleF rect = new RectangleF(e.MarginBounds.Location, e.MarginBounds.Size);
            float top;
            float lineHeight = fNorm.GetHeight(e.Graphics);
            float linesPerPage = e.MarginBounds.Height / lineHeight - (fBoldU.GetHeight(e.Graphics)-fNorm.GetHeight(e.Graphics));
            
            e.Graphics.MeasureString(kunde + " [" + knummer + "]", fNorm, e.MarginBounds.Size, StringFormat.GenericTypographic, out CharsNotNeeded, out Lkunde);
            e.Graphics.MeasureString(content, fNorm, e.MarginBounds.Size, StringFormat.GenericTypographic, out CharsNotNeeded, out Lcontent);
            e.Graphics.MeasureString(bezeichnung, fNorm, e.MarginBounds.Size, StringFormat.GenericTypographic, out CharsNotNeeded, out Lbezeichnung);
            e.Graphics.MeasureString(text, fNorm, e.MarginBounds.Size, StringFormat.GenericTypographic, out CharsNotNeeded, out Ltext);
            Cerfasst = e.Graphics.MeasureString(erfasst, fNorm, e.MarginBounds.Size, StringFormat.GenericTypographic, out CharsNotNeeded, out Lerfasst).Width;
            Cerledigt = e.Graphics.MeasureString("Erledigt: " + erledigt + " [" + erlsupporter + "]", fNorm, e.MarginBounds.Size, StringFormat.GenericTypographic, out CharsNotNeeded, out Lerledigt).Width;

            // Print Customer Information
            top = e.MarginBounds.Top;
            e.Graphics.DrawString(kunde + " [" + knummer + "]", fBoldU, Brushes.Black, e.MarginBounds.Left, top, StringFormat.GenericTypographic);
            if (erledigt.Length > 0 && erlsupporter.Length > 0)
            {
                e.Graphics.DrawString("Hotli ID: " + id, fNorm, Brushes.Black, e.MarginBounds.Right - Cerledigt, top, StringFormat.GenericTypographic);
                e.Graphics.DrawString("Erfasst: " + erfasst, fNorm, Brushes.Black, e.MarginBounds.Right - Cerledigt, top + lineHeight, StringFormat.GenericTypographic);
                e.Graphics.DrawString("Erledigt: " + erledigt + " [" + erlsupporter + "]", fNorm, Brushes.Black, e.MarginBounds.Right - Cerledigt, top + 2 * lineHeight, StringFormat.GenericTypographic);
            }
            else
            {
                e.Graphics.DrawString("Hotli ID: " + id, fNorm, Brushes.Black, e.MarginBounds.Right - Cerfasst, top, StringFormat.GenericTypographic);
                e.Graphics.DrawString("Erfasst: " + erfasst, fNorm, Brushes.Black, e.MarginBounds.Right - Cerfasst, top + lineHeight, StringFormat.GenericTypographic);
                e.Graphics.DrawString("Erledigt: " + erledigt + " [X]", fNorm, Brushes.Black, e.MarginBounds.Right - Cerfasst, top + 2 * lineHeight, StringFormat.GenericTypographic);
            }

            // Print Info
            top += 5 * lineHeight; // --> top + 4 lines +3 points
            e.Graphics.DrawString(bezeichnung, fNorm, Brushes.Black, e.MarginBounds.Left, top, StringFormat.GenericTypographic);

            top += lineHeight; // --> + new line
            e.Graphics.DrawLine(new Pen(Color.Black, 1), e.MarginBounds.Left, top + 3, e.MarginBounds.Right, top + 3); // + 3 Points on bottom

            top += 2 * lineHeight + 3;
            rect.Offset(60, top - e.MarginBounds.Top);
            rect.Width = rect.Width - 60;
            e.Graphics.DrawString("Text: ", fNorm, Brushes.Black, e.MarginBounds.Left, top, StringFormat.GenericTypographic);
            e.Graphics.DrawString(text, fNorm, Brushes.Black, rect, StringFormat.GenericTypographic);
            rect.Offset(-60, -(top - e.MarginBounds.Top));

            top += Ltext * lineHeight + lineHeight;
            rect.Offset(60, top - e.MarginBounds.Top);
            e.Graphics.DrawString("Lösung: ", fNorm, Brushes.Black, e.MarginBounds.Left, top, StringFormat.GenericTypographic);
            e.Graphics.DrawString(content, fNorm, Brushes.Black, rect, StringFormat.GenericTypographic);
            rect.Offset(-60, -(top - e.MarginBounds.Top));
            
            /*int currentLines = 5 + Lbezeichnung + 1 + 2 + Ltext + 1 + Lcontent;
            if (currentLines > linesPerPage)
                e.HasMorePages = true;*/
        }


------------------------------------------------------------------------

2nd Problem:
I have a PrintPreviewControl wher I set "AutoZoom" to false, because I would like to adjust my zoom when clicking on a particular button.
I know how to set the zoom to a defined percentage, but is there a possibility to calculate the best fitting zoom for the page? In fact what I am missing is how to determine the with, height and bounds of my shown paper?

Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Some problems understanding how to use e.HasMorePages and setting zoom

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 22 March 2013 - 12:45 AM

It is fairly simple to think about, this event is fired until e.HasMorePages = False
while (e.HasMorePage)
{
 RaiseEvent PrintPage( ) 
 // draw your page via e.Graphic
 // Have done printing? The last Page
 // the set e.HasMorePage = False
}



It is fairly simple to think about, this event is fired until e.HasMorePages = False
while (e.HasMorePage)
{
 RaiseEvent PrintPage( ) 
 // draw your page via e.Graphic
 // Have done printing? The last Page
 // the set e.HasMorePage = False
}


Was This Post Helpful? 0
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 215
  • Joined: 25-April 11

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 22 March 2013 - 01:25 AM

Not sure about your post... e.HasMorePage is not set at the moment, so it is never "true"!
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 22 March 2013 - 01:44 AM

So that tells you that e.HasMore = False.
Then it doing something like.
Do
 RaiseEvent PrintPage
Until e.HasMorePages = false


So if the event could be called multiple times and it doesn't till which page (via any of the eventargs), what does it suggest you need to do? Keep track your self.

Spoiler

This post has been edited by AdamSpeight2008: 22 March 2013 - 01:45 AM

Was This Post Helpful? 0
  • +
  • -

#5 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 215
  • Joined: 25-April 11

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 22 March 2013 - 04:19 AM

wtf... this is exactly my initial question... lol :D

I understand that if I let the script as it is now (not commented), it generates million of pages, because the HasMorePages-property is never set back to false. But how the hell can I determine or which event should I use to find out, if my e.Graphics.DrawString has reached the bottom MarginBound and because of this the HasMorePages-property should be set to true and again back to false when the content of my second page is printed?
Is there a property that I can use within a while-loop, that tells me the location where my actual e.Graphics.DrawString is drawing to? If so, I could check if it has reached the lower MarginBound and change HasMorePages to true, couldn't I?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 22 March 2013 - 04:43 AM

Why are you having such a difficult time understanding such a simple concept?!

It'll print 1 (One) and only one page because e.HasMorePages = false, and because haven't set it within the method to e.HasMorePages = true. Thus indicating to the caller that you (the callee) have more pages to print.

e.HasMorePage is set to false by the event raiser, each time it is raised.
Use the debugger and look.

This post has been edited by AdamSpeight2008: 22 March 2013 - 04:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 215
  • Joined: 25-April 11

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 03:49 AM

I understand this concept. I know that e.HasMorePages is set to false each time it is raised. What I do not understand is how to tell him at the RIGHT moment, that it has to add a new page and continue printing where it had left?

Lets say my string I want to print has 100 lines but on one page there can be printed only 60. The whole string is stored in a variable, let's say strToPrint, and there are paragraphs of different sizes inside. How can I know, while printing the document, when line 60 is reached, so that I can set e.HasMorePages to true and then continue printing on line 61?

EDIT:
I tried different things, for example splitting my string each time an \n occurs but oviously it can't work because the lines it would get are not the same it would get by using e.Graphics.MeasureString()

This post has been edited by Anthonidas: 23 March 2013 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 05:58 AM

It's not magic. :withstupid:/>
It obvious (to me) that you don't understand the process.

It is delegating the task to you.
YOU have to keep track, what page you are printing.
YOU have to decide what you want printing on that page.
YOU have to decide if you are requesting more pages.

By setting e.HasMorePage = True or setting it to e.HasMorePages = false doesn't trigger any events in the caller. It is just an indicator that caller uses after the printpage event handler method is completed, to check if it as to run the print loop ( which does the actual raising) again.


Start Of Method (where it starts, each time event is raised)
Select Case PageNoe
  Case 1: Do this for Page 1. 
       You print lines  1 to 60 (of 200)
         Set e.HasMorePages = True
        you exit the Method  ( The Caller raises the event again. )
  Case 2: Do this for Page 2
       You print lines 61 to 120 (of 200)
                 Set e.HasMorePages = True
        Method Ends. ( The Caller raises the event again. )
  Case 3: Do this for Page 3
       You print lines 121 to 180 (of 200)
                 Set e.HasMorePages = True
        Method Ends. ( The Caller raises the event again. )
  Case 4: Do this for page 4
       You print lines 181 to 200 (of 200)
                 Set e.HasMorePages = False
        Method Ends.


( The Caller does not raise the event again. )

Done printing multiple pages. Simples

This post has been edited by AdamSpeight2008: 23 March 2013 - 06:11 AM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 06:56 AM

Maybe the OP's problem isn't so much about printing as understanding *documents* are more than just one long string.

Quote

Lets say my string I want to print has 100 lines but on one page there can be printed only 60. The whole string is stored in a variable, let's say strToPrint, and there are paragraphs of different sizes inside.


Adam: I notice you're the only one having put out any code. Maybe its time to sit back and see if Anthonidas comes back with even a vague effort to impliment your suggestions. Otherwise its starting to look like feigning ignorance in an effort to get someone to 'gimme teh codez'

Anthonidas: Make an effort now. You've been given good advice and concepts. Its time for good old fashion trial and error. So much is learned that way.
Was This Post Helpful? 0
  • +
  • -

#10 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 215
  • Joined: 25-April 11

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 11:45 AM

ehm... guys... I really appreciate your efforts in helping me... but perhaps my english is soooo bad, that you still do not understand what my problem really is. In addition, I did not ask for any code at all... just to make it clear, the code I posted previously (my first post) is not copied from anywhere and I really don't want to get any code from you, to just copy & paste!!
This is not going to be an application I will sell or give to others... It is just a test for me, where I'm trying out different technologies I never used before.

Unfortunately your post does not help me at all, because I know already everything you wrote... BUT!! my big problem is just one step before of what you told me to do.

I try to explain it again:
  • I have a very long text to print... different variables in my case.
  • I have a method that puts all pieces together, called PDocument_PrintPage() (see my first post)
  • Within my method I have different e.Graphics.MeasureString()'s. So at the end I know that my text has n lines.
  • Now I should consider of creating a loop to start printing until the maximum of lines per page is reached. This is the point... just one step before of my problem!
  • Let's have a look into a possible loop, to figure out my understanding difficulties:


(This is just an example how I think it could work, ok?)
string strToPrint = "This is my string this is my string, this is my string this is my string. this is my string this is my string this is my string.";
int lastPrintedLine = 0;

void PDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    for (/*not needed here*/; lastPrintedLine <= (lastPrintedLine + maxLinesPerPage); lastPrintedLine++)
    {
        // IS HERE A POSSIBILITY TO BREAK MY TEXT INTO LINES, IN ORDER TO KNOW WHICH LINE IT IS PRINTING AT THE MOMENT??? THIS IS MY PROBLEM!!!
        // I do not know where the current line starts and where this line ends up, so how can I determine what my method is doing at the moment? It will not print from the first "This is my string" to the dot, because this sentence could be too long for one line. Or perhaps it is too short, so it has to add some more text. --> This is what i wanted to know. Here is where I'm having a really difficult time understanding what to do.
    }
}


Again, just to repeat my difficulty: I know that I have to print from line 1 to 60 and then set the e.HasMorePages to true and then go on with the other pages, but at this moment I do not know, WHERE in my text line 60 is. This line 60 could be in the very middle of a paragraph, couldn't it? Or it would also be possible, that this line 60 is exactly 5 words before the end of my text... how to dinamically get this information out?

I hope now it is clear. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,561
  • Joined: 05-May 12

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 12:07 PM

Simple. You use a class variable. It pseudo code:
class MyClass
{
    int _currentPrintLine;
    PrintDocument _pd;

    void printButton_Click()
    {
        _currentPrintLine = 0;
        _pd.Print();
    }

    void pd_PrintPage(sender, printEventArgs)
    {
        while (_currentPrintLine < _maxLines)
        {
            line = _lines[_currentPrintLine];
            if (!line.CanStillFitOnPage(printEventArgs))
                break;
            line.RenderLine(printEventArgs.Graphics)
            ++_currentPrintLine;
        }

        printEventArgs.HasMorePages == _currentPrintLine < _lines.Count;
    }
}



I'll leave it to you as an exercise of how to handle the special case where a single line is too big to fit on a page.

See also: http://msdn.microsof...ntdocument.aspx

As for doing the line breaking, I recommend picking up Petzold's Programming Windows in C#. It has exactly the code you are looking for, but essentially, it's a matter of just incrementing a character index instead of a line index like I was doing above.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 12:49 PM

I'm going to point out again that you guys are having a basic concept miscommunication.

All the help is coming from a concept of having several lines of text.
16 line = _lines[_currentPrintLine]; // an array of lines

The OP doesn't have several lines. He has one giant string.

Quote

Lets say my string I want to print has 100 lines but on one page there can be printed only 60. The whole string is stored in a variable


He's asking how he can break the single string at the end of the page, and continue the rest of the string on the next page.

His problem isn't printing - Its how he is storing the data to begin with.

This post has been edited by tlhIn`toq: 23 March 2013 - 12:51 PM

Was This Post Helpful? 1
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,561
  • Joined: 05-May 12

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 01:14 PM

Actually you were write clear about him having a single string. I don't want to give the game away by addressing the line breaking directly, but at the same time I wanted to show that it is relatively easy to support the logic for determing has more pages. In Petzold's book he only deals with a single string and shows how to do the line breaking. In my code above, _lines could be a facade in front of the single string where the indexer function does the line breaking.

My intent was to show that it is doable and need not be overly complex.

Sent from my T-Mobile G2 using Tapatalk 2

This post has been edited by tlhIn`toq: 23 March 2013 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 23 March 2013 - 02:04 PM

View PostSkydiver, on 23 March 2013 - 02:14 PM, said:

Sent from my T-Mobile G2


I'm so sorry for you.
Was This Post Helpful? 0
  • +
  • -

#15 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 215
  • Joined: 25-April 11

Re: Some problems understanding how to use e.HasMorePages and setting zoom

Posted 31 March 2013 - 03:23 PM

Ok guys finally I got it... (without help of anyone else)

I do not know if this is the correct way of doing it, but it works... ;)

I splitted my string by the char ' ' (space) and looped then trough the length of the new array, measuring by each array item if it is less than the maxLinewsPerPage. Then I raise the e.HasMorePages and do the same thing again... Is this correct?

here is just a snipet, so you could find some not declared variables, because they are declared in an other place:
char[] delimiters = new char[] { ' ' };
string[] newText = contentArray[5].Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

int j = lastPrintPosition;

if (j > 0)
{
    // Reset y-Position of printingRectangle!!
    printRectangle.Offset(0, -yOffset);
    alreadyPrintedLines = 0;
}

lastPrintPosition = 0;

for (; j < newText.Length; j++)
{
    e.Graphics.MeasureString(currentlyPrintingText + newText[j] + " ", fontNormal, printRectangle.Size, StringFormat.GenericTypographic, out notNeeded, out currentTextLines);
    if (alreadyPrintedLines + currentTextLines < linesPerPage)
    {
        currentlyPrintingText += newText[j] + " ";
    }
    else
    {
        lastPrintPosition = j;
        break;
    }
}
e.Graphics.DrawString(currentlyPrintingText, fontNormal, Brushes.Black, printRectangle, StringFormat.GenericTypographic);

if (lastPrintPosition > 0)
{
    e.HasMorePages = true;
    currentlyPrintingText = "";
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2