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