0

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();
MaxW
  • 421
  • 1
  • 7
  • 22
  • is pdfsharp able to interpret html tags? – Mong Zhu Aug 02 '16 at 08:35
  • I am not really sure but its the best at the moment for my my situation exept the html strings. – MaxW Aug 02 '16 at 08:37
  • I think usually you would do it by hand. There are methods in PdfSharp to make a `Section` and add `Paragraphs` to the page and structure it this way. Have a look at [this](http://www.pdfsharp.net/wiki/invoice-sample.ashx) – Mong Zhu Aug 02 '16 at 08:40
  • I know that and it would work but the text can change and i don´t want to make multiple PDFs. – MaxW Aug 02 '16 at 08:46

2 Answers2

1

Unfortunately PDFSharp does not support HTML-Tags:

Have a look at the answers in this post. You would need to do it by hand, using the methods that PDFSharp offers.

EDIT: If you need to preserve the HTML-Tags you might use a Converter.

Here is quite an old post concerning this topic.

For PDFSharp the answers in this post might help

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

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.

MaxW
  • 421
  • 1
  • 7
  • 22