2016-08-02 91 views
0

我使用richtextbox创建文本块并将它们保存到数据库。之后,我尝试用字符串创建一个PDF。PDFSharp解析富文本字符串

我的问题是,我不能找到一种方法来显示在PDF格式的字符串,它显示每个< p>等,而不是正确使用它们。如果使用PDFsharp不可能,还有其他可能性吗?

// 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(); 
+0

是pdfsharp能够解释html标签吗? –

+0

我不是很确定,但是现在对我的情况来说,它是最好的,可以免除html字符串。 – MaxW

+0

我觉得通常你会用手做。 PdfSharp中有一些方法可以创建一个'Section'并在页面中添加'Paragraphs'并以这种方式构造它。看看[这](http://www.pdfsharp.net/wiki/invoice-sample.ashx) –

回答

0

所以我找到了答案。 PDFSharp有XTextFormatter tf = new XTextFormatter(gfx); 它支持\n \t \r所以我有替换我的richtextbox女巫普通的文本框。

1

可惜PDFSharp不支持HTML标签:

看一看在这个post的答案。您需要使用PDFSharp提供的方法手工完成。

编辑:如果您需要保留HTML标签,您可能会使用Converter

Here是一个相当老的帖子关于这个话题。

对于PDFSharp在此post的答案可能会帮助

+0

手动更改文字并非完美解决方案:/ – MaxW

+0

我编辑了我的答案,似乎有一些解决此问题的方法。 –

+0

hm好吧看起来好像我会尝试找到其他可能性来创建我的文本 – MaxW