我使用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ü 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();
是pdfsharp能够解释html标签吗? –
我不是很确定,但是现在对我的情况来说,它是最好的,可以免除html字符串。 – MaxW
我觉得通常你会用手做。 PdfSharp中有一些方法可以创建一个'Section'并在页面中添加'Paragraphs'并以这种方式构造它。看看[这](http://www.pdfsharp.net/wiki/invoice-sample.ashx) –