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

Measuring multiline strings
https://forum.pdfsharp.net/viewtopic.php?f=2&t=237
Page 1 of 1

Author:  cmosses [ Fri Oct 05, 2007 9:19 am ]
Post subject:  Measuring multiline strings

Hello, great component, but I have this problem:

I need to draw a number of items in a list, and the list is quite narrow, so the items typically don't fit on a single line. I would like to measure how many lines a text fills when wrapped, but XGraphics.MeasureString does not have an overload that lets me specify maximum line width. It can only tell me the width of the text on a single line, not when broken into multiple lines.

I can do an XGraphics.DrawString and specify a layout rectangle, and that works fine, wrapping the lines perfectly, but afterwards I have no idea how many lines were filled. Thus, I don't know where to start the next list item on the page.

Is there any other way I could do this? Any ideas?

Can anyone please help? Thanks a lot in advance.

Author:  mohali [ Tue Oct 23, 2007 7:24 pm ]
Post subject: 

I've a same problem. Did you measure no of lines. I need to know when the what is next line. Also when string is long.

Thanks,

-mak

Author:  cmosses [ Wed Oct 24, 2007 1:50 am ]
Post subject: 

mohali wrote:
I've a same problem. Did you measure no of lines. I need to know when the what is next line. Also when string is long.

Thanks,

-mak


I wrote a method as a workaround, which reliably measures multiline strings within a maximum layout rectangle. I use the primitive MeasureString that we are given, measuring a single line as I gradually add words to the end of it. When it is full within the max line width, I start a new line and start measuring again, and so forth. The height is the number of lines times the font height. Hope this helps.

Author:  mohali [ Wed Oct 24, 2007 1:05 pm ]
Post subject: 

can you give me sample code?

thanks,

rgds,

-mak

Author:  cmosses [ Tue Oct 30, 2007 1:18 am ]
Post subject: 

Here you have the code that will do this:
PDFsharp Multiline MeasureString

Author:  ACS [ Sat Dec 08, 2007 2:43 am ]
Post subject: 

Thanks for the code, cmosses.
However there is a problem with that code that causes the method to return the wrong height (it will be off by 1 lineCount.)

The bug is not with the code itself, but rather with the .NET Framework's MeasureString method, which is used by PDFSharp's MeasureString method. The problem is that MeasureString will automatically trim the string being measured, eliminating any spaces. This means that " Hello, world! " will be measured as "Hello, world!". This is not a problem for single-line strings, but for multiple-line strings it may cause incorrect results.

I discovered this recently during a project and I found a cheapo way to resolve it, and that is by using a period character instead of a space, which is approximately the same width (at least with the few fonts that I've tested.) This will result in perfect MeasureString results each time.

If anyone's interested in this code, let me know and I'll post it.

Author:  cmosses [ Thu Dec 13, 2007 8:43 am ]
Post subject: 

ACS wrote:
Thanks for the code, cmosses.
However there is a problem with that code that causes the method to return the wrong height (it will be off by 1 lineCount.)

The bug is not with the code itself, but rather with the .NET Framework's MeasureString method, which is used by PDFSharp's MeasureString method. The problem is that MeasureString will automatically trim the string being measured, eliminating any spaces. This means that " Hello, world! " will be measured as "Hello, world!". This is not a problem for single-line strings, but for multiple-line strings it may cause incorrect results.

I discovered this recently during a project and I found a cheapo way to resolve it, and that is by using a period character instead of a space, which is approximately the same width (at least with the few fonts that I've tested.) This will result in perfect MeasureString results each time.

If anyone's interested in this code, let me know and I'll post it.


Pass an XStringFormat with the flag XStringFormatFlags.MeasureTrailingSpaces to solve this (I think).

Author:  ilivewithian [ Wed May 07, 2008 1:55 pm ]
Post subject: 

I've noticed a small bug with the multiline MeasureString, it doesn't take into account \n in strings, so the number of lines will be wrong if a new line is in the code.

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