PDFsharp Line Break

Afnan Bashir picture Afnan Bashir · Mar 15, 2011 · Viewed 27.1k times · Source

I am trying to get new line but if I use \n it does not work.

Any way to have new line by adding something to string like \r\n (which also does not work)

gfx.DrawString("Project No \n" + textBoxProjNumber.Text, fontUnder, XBrushes.Black, 230, 95);

(the example snippet shows what I've tried but does not work).

Answer

Have you tried the XTextFormatter class?

See here: http://www.pdfsharp.net/wiki/TextLayout-sample.ashx

Code snippet:

PdfDocument document = new PdfDocument();

PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);
XTextFormatter tf = new XTextFormatter(gfx);

XRect rect = new XRect(40, 100, 250, 220);
gfx.DrawRectangle(XBrushes.SeaShell, rect);
tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft);