Search code examples
c#pdfsharprichtext

PDFSharp parsing rich text string


I create text blocks using a richtextbox and save them to database. After that i am trying to create a PDF with the strings.

My problem is that i cant find a way to display that formated string in the PDF, it shows every < p > etc. instead of using them correctly. If it is not possible with PDFsharp are there other possibilitys?

 // Create a new PDF document
 PdfDocument document = new PdfDocument();

 // Create an empty page            
 PdfPage oPage = document.AddPage();

 // Get an XGraphics object for drawing
 XGraphics gfx = XGraphics.FromPdfPage(oPage);

 //Drawing Images and Rectangles and other things


 //sText is just an example of what my DB string looks like 
 string sText = "<p>Here you can see formattet text<\p> \n 
 always multiple rows and some  g&uuml;  or /r"


 gfx.DrawString(sText, NormalText, XBrushes.Black, new XRect(XUnit.FromCentimeter(3.2), XUnit.FromCentimeter(16.35), oPage.Width, oPage.Height), null);


 // Send PDF to browser
 MemoryStream stream = new MemoryStream();
 document.Save(stream, false);
 Response.Clear();
 Response.ContentType = "application/pdf";
 Response.AddHeader("content-length", stream.Length.ToString());
 Response.BinaryWrite(stream.ToArray());
 Response.Flush();
 stream.Close();
 Response.End();

Solution

  • So i found an answer. PDFSharp has XTextFormatter tf = new XTextFormatter(gfx); which supports \n \t \r so i have do replace my richtextbox witch a normal textbox.