PDFSharp: Measuring height of long text with word wrap

c#, pdf, pdf-generation, pdfsharp

Solution

I found this extension of PdfSharp to be the answer to this problem:

http://developer.th-soft.com/developer/2015/07/17/pdfsharp-improving-the-xtextformatter-class-measuring-the-height-of-the-text/

You can clone or fork the relevant code here:

https://github.com/yolpsoftware/PdfSharp/tree/measure-text-height

Problem

PDFSharp supports automatic text wrapping when drawing long text portions: ``` textFormatter.DrawString(text, font, XBrushes.Black, new XRect(x, y, textAreaWidth, 1000), XStringFormats.TopLeft); ``` This will wrap the text if it is longer than `textAreaWidth`. How can I get the height of the text that has just been drawn? I tried it with `gfx.MeasureString()`, but there is no overload that supports specifying a maximal width. `gfx.MeasureString()` returns the size of the text without text wrapping. Thanks for any hints.

Original source