问题描述
图书馆是否 PDFSharp 能 - 例如 iTextSharp的 - 生成PDF文件*的考虑到HTML格式* 的? (粗体(强),间距(BR),等等。)
Does the library PDFSharp can - like iTextSharp - generate PDF files *take into account HTML formatting *? (bold (strong), spacing (br), etc.)
以前我用下面的 iTextSharp的的和大致以这样的方式处理(代码):
Previously I used iTextSharp and roughly handled in such a way (code below):
string encodingMetaTag = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
string htmlCode = "text <div> <b> bold </ b> or <u> underlined </ u> <div/>";
var sr = new StringReader (encodingMetaTag + htmlCode);
var pdfDoc = new Document (PageSize.A4, 10f, 10f, 10f, 0f);
var = new HTMLWorker htmlparser (pdfDoc);
PdfWriter.GetInstance (pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open ();
htmlparser.Parse (sr);
pdfDoc.Close ();
纳入相应的HTML表单到PDF文档处理类对象的 HTMLWorker ..所以什么用的 PDFSharp 的? 的PDFSharp类似的解决方案的
incorporated into the appropriate HTML form to a PDF document dealt with the class object HTMLWorker.. so what with PDFSharp? Has PDFSharp similar solution?
推荐答案
我知道这个问题是老,但这里有一个干净的方式来做到这一点...
I know this question is old, but here's a clean way to do it...
您可以使用的与组合来实现:
You can use HmlRenderer in combination with PDFSharp to accomplish this:
Bitmap bitmap = new Bitmap(1200, 1800);
Graphics g = Graphics.FromImage(bitmap);
HtmlRenderer.HtmlContainer c = new HtmlRenderer.HtmlContainer();
c.SetHtml("<html><body style='font-size:20px'>Whatever</body></html>");
c.PerformPaint(g);
PdfDocument doc = new PdfDocument();
PdfPage page = new PdfPage();
XImage img = XImage.FromGdiPlusImage(bitmap);
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(img, 0, 0);
doc.Save(@"C:\test.pdf");
doc.Close();
有人报告说,最终的图像看起来有点模糊,显然是由于自动反锯齿。下面是关于如何解决一个帖子消息:
这篇关于基于HTML代码,生成PDF(iTextSharp的,PDFSharp?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!