I've gotten page breaks to work in PdfSharp, but I'm still not managing to get line breaks to behave.
In this particular case, I want to draw the string "Department Name" in 12-point bold underline
Arial, in a space no wider than 90 pt.
So, let's get started:
Code:
XGraphics sizer = XGraphics.FromPdfPage(new PdfPage(new PdfDocument()); // Just a sizer; I'm not drawing anything yet.
sizer.MeasureString("Department Name", new XFont("Arial", 12, XFontStyle.Bold | XFontStyle.Underline));
sizer.MeasureString("Department\nName", new XFont("Arial", 12, XFontStyle.Bold | XFontStyle.Underline));
The former returns (102.7, 13.4). This is too wide, so I replace the space with a carriage return, and try again. The latter returns a width less than 90 pt: (66.7, 27.6). This is good. This is expected. The height approximately doubled, and the width is within the required 90 pt. However, when I go to draw it:
Code:
XGraphics canvas; // initialized appropriately, so I can actually save the document when I'm done.
canvas.DrawString("Department\nName", new XFont("Arial", 12, XFontStyle.Bold | XFontStyle.Underline), XBrushes.Black, 72, 72)
I get this gem:
Attachment:
DepartmentName.PNG [ 1.43 KiB | Viewed 35674 times ]
(This is made worse by the string that starts 90 pt to the right; smack on top of the 'm'.)
The obvious issue is the non-existence of the line break, but note also the approximately em-sized space where the "\n" was, and the underlining that disappears.
Using "\r" instead if "\n" is identical, except that the underlining is preserved. (And that MeasureString returns a different value.)
Using "\r\n" adds an em-sized underlined space immediately after the 't', and before the non-underlined space.
Using "\v\n" measures with substantially more vertical space, but again fails draw the text on multiple lines.
What do I need to do to get DrawString to draw in the box that MeasureString says it will?
[0] For the record, I can also handle line breaks for strings like "DepartmentName". "DepartmentNa\nme" doesn't print any better than "Department\nName".