2

from these two questions in SO | 1 | and | 2 |(this one's my own) I've tried to solve but I'm running into some problems.

Take a look at where I am stuck!!

  1. I have successfully word-wrapped this string "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" using a code (see my SECOND CODE)
  2. But I DO NOT KNOW how to get the number of lines that have been wrapped. (because it wraps automatically).
  3. This is the screenshot of my PRINT PREVIEW:

    Thats my problem. The word-wrap is happening but NOT the line spacing!

    I want to put some space between Residential Address and Permanent Address. To do that I need to know how mny lines are being word-wrapped.

    Thats my problem. The word-wrap is happening but NOT the line spacing! I just want to know how I can calculate the number of lines that have been word-wrapped so that I can run a function to do appropriate line spacing between two fields!

  4. I want to know the number of lines that have been word-wrapped (which you can observe, in the screenshot above, is clearly = 2 here).
  5. Why do I need this number? ==> If x is the number of lines word wrapped, I want to execute the function newline() x number of times. That's why. Its my own function whose purpose is to correct line spacing among different fields.

Example:

  • For a string "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    • IF the page margin only allows 10 characters per line then

      • The output should be

        ABCDEFGHIJ
        KLMNOPQRST
        UVWXYZ
      • and the number of lines used, stored by variable linesFilled(say), should be = 3
  • Obviously,this is just a run-down example. In real-practice, I would like to have no character limit per line. Instead I want MeasureString to automatically know how many words fit in a line and then word-wrap the rest that are not fitting to next consecutive line(s). For your information: I have already done this much . You see, I seek your help only to know how to get the number of lines that have word-wrapped. That has been really tricky to work around.

What I have tried so far:

FIRST CODE:

My code looks like this(for the line counting; which you need to help me with):

int charactersFitted;

int linesFilled;

SizeF stringSize = new SizeF();

stringSize = e.Graphics.MeasureString("Residential Address: " + 
                RAddressTextBox.Text, stringFont, layoutSize, newStringFormat, 
                out charactersFitted, out linesFilled);
textBox1.Text = Convert.ToString(stringSize);
textBox2.Text = Convert.ToString(stringSize.Width);

So, this first code is supposed to give me the number of lines that were wrapped around the print margin. Currently just gives the width of the part of string that occupied the whole line as opposed to the number of lines the string has occupied (can I get a method to know the number of lines?)

SECOND CODE:

Graphics RAddress = e.Graphics;

SizeF RAddressLength = RAddress.MeasureString("Residential Address: "+ 
RAddressTextBox.Text, stringFont,700);

RAddress.DrawString("Residential Address: " + RAddressTextBox.Text,
stringFont, Brushes.Black, new RectangleF(new Point(pagemarginX,newline()),
RAddressLength), StringFormat.GenericTypographic);

and this second code helps we actually wrap the string when it does not fit in page margin(this second code here works perfectly at the moment. It automatically word-wraps text NOT fitting in a line to next consecutive line(s) but it DOES NOT tell me how many lines have been word-wrapped. THATS my problem)

Note: newline() is my own function which leaves one line when called. And pagemarginX sets approapirate margin. Thats all. Do not be confused. As for why I havent used DrawString in my FIRST CODE; I have been using both codes. This one to display the string and the FIRST CODE to count the lines in string. I haven't been able to count the number of lines with this one. Sorry for the confusion.



SAMPLE OUTPUT(s) FOR YOUR INFORMATION:

Currently, output of stringSize.Width is 114.226.

As suggested in some of the comments,
I tried outputting linesFilled instead of stringSize.Width and the output was 5 .

Another suggestion was to try int numLines = Convert.ToInt32(Math.Ceiling(layoutSize.Width / stringSize.Width)); which gave me the output 7 .

As shown in the screenshot of my PRINT PREVIEW over there^^^, I obviously need the output=2 for my string. Please somebody help me!


I welcome:

  • Solutions that augment and enhance my FIRST CODE to produce correct lines of word-wrap as output for the string: "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" (which should be 2)
  • Solutions that can modify my SECOND CODE to also produce the number of lines that have been wordwrapped (This type of solution would be awesome!!)
  • Creative solution: A solution that takes input as string and wordwraps that string to a margin and also produces the number of lines word-wrapped. (OR, whatever you think that solves this problem also works!)

My specifications are NOT rigid. You can solve this problem any way you are comfortable with! I have edited my question to include as much detail as possible. If you'd like the whole module, you can simply ask me too. I am hoping for a solution. Thanks!

Community
  • 1
  • 1
Siddhant Rimal
  • 975
  • 2
  • 11
  • 32
  • Sounds like a job for a StringBuilder... just loop over each character and put a linebreak and increment the count every 80 or whatever characters. – Casey Apr 12 '16 at 18:46
  • If you want to keep words together you'll need a slight bit more sophistication but the principle is the same. – Casey Apr 12 '16 at 18:46
  • @Casey : sounds interesting. I have been trying to process whole strings and I never gave consideration to character manipulation. That would surely work for this specific purpose. I'll keep his suggestion as a fallback option if nothing else works – Siddhant Rimal Apr 13 '16 at 05:50
  • Your first code has the number: linesFilled – LarsTech Apr 13 '16 at 19:03
  • @LarsTech: I realise that and if you would read the **sample outputs** I have given over there, you'd know that it returned the output as 5. My string possibly cannot return a wordwrap line count as 5 as it is visually observed as 2. Maybe I'm doing some mistake with layoutSize which I have set as (700.0F,200.0F). while my second code has 700px(I assume its pixels or some other unit that aligns with all my other calculations). – Siddhant Rimal Apr 13 '16 at 19:09
  • You haven't posted code that duplicates that problem. Version 1 shows no DrawString. Version 2 shows you measuring and drawing two different strings. Post sample code that duplicates the problem 100% for us, so that we can test it, too. – LarsTech Apr 13 '16 at 19:11
  • I have been using this exact string all along "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" . I want to word-wrap this string(which I have done in SECOND CODE) and then I want to know how many lines have been used to wordwrap.(I can see its 2 with my current fontsize and all but I cant seem to output that number to any variable). This much information should be sufficient to duplicate my result. Can you help me? @LarsTech – Siddhant Rimal Apr 13 '16 at 19:23
  • `"Residential Address: " + linesFilled + RAddressTextBox.Text` is not what you measured. I can't duplicate your problem. I get the number 2 with my code. – LarsTech Apr 13 '16 at 19:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109080/discussion-between-lancel0t-and-larstech). – Siddhant Rimal Apr 13 '16 at 19:30

2 Answers2

0

I think you meant

textBox2.Text = Convert.ToString(linesFilled);

instead of

textBox2.Text = Convert.ToString(stringSize.Width);

Edit: Try this:

int numLines = Convert.ToInt32(Math.Ceiling(layoutSize.Width / stringSize.Width));
smead
  • 1,768
  • 15
  • 23
  • I'm afraid, No. You see, I've tried it before. I've tried numerous things. When I output **textBox2.Text = Convert.ToString(linesFilled);** , I get an **output: 5** but I can clearly see my line overflowing to one next line only. The **output should have been 2** but I have been unable to get that output for sometime now. – Siddhant Rimal Apr 13 '16 at 05:36
  • New method **outputs:7** instead of **2**. If I subtract the linesFilled rom this number I'd get my answer but maybe this is just a coincidental answer. I need to test it for a bit lol. Thanks for trying :) – Siddhant Rimal Apr 13 '16 at 05:53
0

Not sure if this is what you're looking for but when using a print dialog you can do something like this that will give you the number of characters on the page and how many lines per page the string takes up:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
        int charactersOnPage = 0;
        int linesPerPage = 0;

        // Sets the value of charactersOnPage to the number of characters 
        // of stringToPrint that will fit within the bounds of the page.
        e.Graphics.MeasureString(stringToPrint, this.Font,
            e.MarginBounds.Size, StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);

    }

e.MarginBounds.Size is what will do the trick for you I believe. Then you can just takes the "charactersOnPage" value and divide it by "linesPerPage" to get the number of characters that fit on one line:

var charactersPerLine = charactersOnPage / linesPerPage;

Once you have "charactersPerLine" you can accomplish the rest of what you're trying to do.

pantuofermo
  • 160
  • 7
  • I've edited my question a hell lot to exactly deliver what I'm looking for. Would you kindly revise your answer. Thanks :) – Siddhant Rimal Apr 13 '16 at 19:10
  • Hmm, if you could somehow run the function I have above once for each sentence (e.g. input this sentence in my function: "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal"), then you could get the "linesPerPage" number and know how many lines that sentence takes up. I will think on this more for a complete solution. – pantuofermo Apr 13 '16 at 21:12
  • my sincerest thanks for trying. I used your code too. Its similar to my first code. However upon thinking about possible ways, I have stumbled upon an extremely simple solution which can be achieved only with the content of second code. Meanwhile, I think I'll now need help for overflowing text between pages. Its 3:05AM over here so I'll excuse myself and post the answer when I'm not sleepy. Have a good day :) – Siddhant Rimal Apr 13 '16 at 21:22