PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Can I get a text measurement after word wrap?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1191
Page 1 of 1

Author:  JeffJohnson [ Fri May 21, 2010 4:36 pm ]
Post subject:  Can I get a text measurement after word wrap?

Let's say I'm putting text into a table in a MigraDoc document. The text is going into a column with a fixed width, 2 inches in this case. I would like to be able to measure how high that row will become after the text is formatted (i.e., after word wrapping takes place). Does MigraDoc provide this ability? If the TextMeasurement class accepted a Rectangle (or a max width) in the MeasrureString() method then I would go that route, but it doesn't.

Author:  935main [ Wed Jun 30, 2010 10:11 pm ]
Post subject:  Re: Can I get a text measurement after word wrap?

Good question - I'm looking for the answer to a very similar question. Did you ever find an answer?

Author:  WouterHuysentruit [ Fri Aug 27, 2010 8:48 pm ]
Post subject:  Re: Can I get a text measurement after word wrap?

Hi Folks,

That's why I created my own Word Wrapping class, so I was able to measure the height of the wrapped textblock. I'm using this class to add text to a table cell, and resize the height of the cell to fit the wrapped content.

Create an instance of the PdfWordWrapper (see attachment) and feed the class with textblocks by calling the Add method. For each call to this method you can supply a different font and/or brush. After all textblocks have been added, call the Process method.

After that you can read the Size property to check the Height of the wrapped text, and use the Draw method to finally draw the output to an XGraphics object.

Example:
Code:
PdfWrodWrapper wrapper = new PdfWordWrapper(g, XUnit.FromMillimeter(100));
wrapper.Add("Title", titleFont, XBrushes.Black);
wrapper.Add("Content sdflm sldkf smdlfk smdflk sdmflksdmflk sdlfm", contentFont, XBrushes.Black);
wrapper.Process();
double height = wrapper.Size.Height;
wrapper.Draw(g, XUnit.FromMillimeter(71), itemTop + XUnit.FromMillimeter(0.5), PdfWrodWrapper.Alignment.Left);


I hope this stuff is of any use :)

Grtz,
Wouter Huysentruit

Attachments:
PdfWordWrapper.zip [1.93 KiB]
Downloaded 735 times

Author:  Krabbie [ Wed Oct 31, 2012 9:30 am ]
Post subject:  Re: Can I get a text measurement after word wrap?

Hi all, I faced a bug in the PdfWordWrapper of Wouter Huysentruit:

If you try to put a large string (without spaces) inside a 'too small' area, it gave an exception. I fixed it with next code change in the CreateBlocks function:

Code:
while (blockSize.Width > width)
{
    for (int i = value.Length - 1; i > 0; i--)
    {
        string part = value.Substring(0, i);

        blockSize = graphics.MeasureString(part, font);
        blockSize.Height = lineHeight;

        if (blockSize.Width > width)
            continue;

        blocks.Add(new Block(part, BlockType.Text, font, brush, blockSize, descent));

        value = value.Substring(i, value.Length - i);

        // *** This is the bug fix *** variable 'i' needs to get the value of the new length of the shortened string
        i = value.Length + 1;  //  will be decremented by 1 on next iteration

        if (value.Length == 0)
        {
            blockSize = XSize.Empty;
            break;
         }
    }
}


I hope this fix will help others,

Greetings, Krabbie

Improved readability - Forum Administration

Author:  ghendric [ Fri Apr 19, 2013 4:58 pm ]
Post subject:  Re: Can I get a text measurement after word wrap?

I need to do this with a TextFrame. I need to calculate the height of it based on long a string of data is. The width of the TextFrame is static and doesn't change. Just the height.

In your example you show that you are passing a XGraphic parameter to the constructor of the class. What value does that hold?

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/