Code:
using PdfSharp.Drawing;
using PdfSharp.Pdf;
namespace PDFSharpTest {
class Program {
static void Main(string[] args) {
// Initialize drawing tools and variables
PdfDocument doc = new PdfDocument();
PdfPage page = doc.AddPage();
XGraphics canvas = XGraphics.FromPdfPage(page);
XFont font = new XFont("Arial", 10);
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
string text = "Test string that\ncontains two\nline-feeds.";
XPoint origin = new XPoint(72, 72);
// Measure the text we're about to draw,
XSize size = canvas.MeasureString(text, font);
// draw a red rectangle,
canvas.DrawRectangle(XPens.Red, new XRect(origin, size));
// and draw the text within that rectangle.
canvas.DrawString(text, font, XBrushes.Black, origin, format);
doc.Save("test.pdf");
}
}
}
The above code does not do what the comments suggest it does. To whit, it measures the text and draws a red rectangle, but then it draws the majority of the text outside that rectangle. The PDF it generates is attached.
Is a case of DrawString handling control characters incorrectly, or merely a case of me not reading the documentation properly?