PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

DrawString for a text with \n
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2230
Page 1 of 1

Author:  kabani [ Tue Nov 20, 2012 1:27 am ]
Post subject:  DrawString for a text with \n

Hello all,
I noticed there were some discussions on this in the past. How to display a text with line breaks,
for example "1\n2\n3\n4\n5", but did not find an answer. Is this possible at all in PDFSharp?
Thanks.
-Kaveh

Author:  Thomas Hoevel [ Tue Nov 20, 2012 9:30 am ]
Post subject:  Re: DrawString for a text with \n

Hi!
Use XTextFormatter class (and don't forget the '\n' in your strings).
viewtopic.php?p=6414#p6414

Author:  kabani [ Tue Nov 20, 2012 4:22 pm ]
Post subject:  Re: DrawString for a text with \n

Dear Thomas,
Thank you for the quick response. I actually saw a sample of XTextFormatter and tried it. Here is the part of the code with hard coded values:
..................
PdfDocument s_document = new PdfDocument();
PdfPage page = new PdfPage();
page = s_document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XStringFormat drawFormat = new XStringFormat();
drawFormat = XStringFormats.TopLeft;
XFont font = new XFont(node.Font.Name, node.Font.Size);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(62, 666, 449, 114);
Node.Text = "1\n2\n3\n4\n5\n6\n7\n8";
gfx.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);
.......................
The output comes in a rectangle as
1 2 3 4 5 6 7 8

So it seems that the \n is converted to tab or several spaces.

Author:  kabani [ Tue Nov 20, 2012 4:26 pm ]
Post subject:  Re: DrawString for a text with \n

Sorry, I forgot the Frawrectangle statement. Here is the corrected code again

PdfDocument s_document = new PdfDocument();
PdfPage page = new PdfPage();
page = s_document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XStringFormat drawFormat = new XStringFormat();
drawFormat = XStringFormats.TopLeft;
XFont font = new XFont(node.Font.Name, node.Font.Size);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(62, 666, 449, 114);
gfx.DrawRectangle(XBrushes.LightYellow, rect);
node.Text = "1\n2\n3\n4\n5\n6\n7\n8";
gfx.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);

Author:  kabani [ Tue Nov 20, 2012 5:34 pm ]
Post subject:  Re: DrawString for a text with \n

Problem sloved, thanks Thomas.

I had a mistake, I used
gfx.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);
instead of using the text formatter
tf.DrawString(node.Text, font, XBrushes.Black, rect, drawFormat);

Thank you very much for the assistance.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/