2016-08-02 87 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