Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I use this code I expect a multiline text:

VB
using (XGraphics gfx = XGraphics.FromPdfPage(page))
{
   string text = "aaa\r\nbbb\ncccc";
   gfx.DrawString(text, font, XBrushes.Black, 20, 20);
}


In fact, when I draw in a winform using GDI+ this text renders in multiple lines. Is there a way to achieve this with PDF Sharp?
Posted
Updated 29-Sep-14 2:55am
v2

1 solution

The XGraphics.DrawString(...) method does not handle line breaks (newlines) as the .NET Graphics.DrawString(…) method does.
Instead, PDFSharp will output something like blanks for \r and \n.

This is a little bit trickier to work around. You need an XTextFormatter, a XRect for the region which is available for the layouter and then call DrawString(…) on the formatter instead of the XGraphics object:

VB
var formatter = new XTextFormatter(pageGraphics);
var layoutRectangle = new XRect(10, 10, page.Width, page.Height);
formatter.DrawString("Hello\r\nWorld", arial, XBrushes.Black, layoutRectangle);


Generating PDFs with PDFSharp[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900