PDFsharp includes a class XTextFormatter that can be used to draw text with linebreaks.
However it can not determine the height needed for the text. Inspired by a comment from @Wakka02 I improved this class, generating class XTextFormatterEx.
In my opinion it also answers the original question, therefore I post an answer.
I know this is an old question and the answer may not help the OP, but it is a frequently asked question and the answer may help others.
The new class has 500 lines of code - and I think this would be too much for this post.
The source code can be found on the PDFsharp forum:
http://forum.pdfsharp.net/viewtopic.php?p=9213#p9213
It can also be found in my humble blog:
http://developer.th-soft.com/developer/pdfsharp-improving-the-xtextformatter-class-measuring-the-height-of-the-text/
When using the new class, you can first call PrepareDrawString
to find out how much of the text fits and which height the fitting text has. Then your decoder can draw the prepared text or prepare another text or prepare the same text with a different rectangle.
My new class at work:
XTextFormatterEx tf = new XTextFormatterEx(gfx);
int lastCharIndex;
double neededHeight;
// Draw the text in a box with the optimal height
// (magic: we know that one page is enough).
XRect rect = new XRect(40, 100, 250, double.MaxValue);
//tf.Alignment = ParagraphAlignment.Left;
tf.PrepareDrawString(text, font, rect,
out lastCharIndex, out neededHeight);
rect = new XRect(40, 100, 250, neededHeight);
gfx.DrawRectangle(XBrushes.SeaShell, rect);
// Both variants should look the same.
// Optimized version: draw the prepared string.
tf.DrawString(XBrushes.Black, XStringFormats.TopLeft);
Preparing the text invokes MeasureString many times. Later the prepared text can be drawn without invoking MeasureString again.
As of today (Juli 17, 2015) the class XTextFormatterEx (like the original XTextFormatter) uses internal fields of the XFont class. This requires special treatment when compiling the class. I decided to copy my XTextFormatterEx class into the PDFsharp folder after downloading the complete source package for PDFsharp 1.32.
Anybody trying to modify either the XTextFormatter or XTextFormatterEx class will face the same problem.
I hope this issue will be solved with future versions of PDFsharp, allowing modified versions of these classes to be included in the application project.