基于HTML代码生成PDF(iTextSharp,PDFSharp?)

Tom*_*Ufx 18 html c# pdf pdfsharp itextsharp

PDFSharp是否可以像iTextSharp一样生成PDF文件*考虑HTML格式*?(粗体(强),间距(br)等)

以前我使用iTextSharp并以这种方式粗略处理(下面的代码):

 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 ();
Run Code Online (Sandbox Code Playgroud)

将适当的HTML表单合并到处理类对象HTMLWorker的PDF文档中..那么用PDFSharp做什么?有PDFSharp类似的解决方案吗?

Die*_*ego 15

我知道这个问题已经过时了,但这是一个干净的方法......

您可以使用HtmlRenderer结合PDFSharp来完成此任务:

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();
Run Code Online (Sandbox Code Playgroud)

有些人报告说最终图像看起来有点模糊,显然是由于自动消除锯齿.这是关于如何解决这个问题的帖子:http://forum.pdfsharp.com/viewtopic.php?f = 2&t = 1811&start = 0

  • 它不会生成本机PDF代码.它是什么将HTML呈现为图像并将图像插入PDF.我不认为这是从HTML生成PDF的正确方法.据我所知,目前还没有可以将html转换为PDF的代码库.你必须自己写一个. (5认同)
  • 检查最新的[HTML Renderer](https://htmlrenderer.codeplex.com),它支持本机PdfSharp渲染。也可以通过NuGet获得:[HtmlRenderer.PdfSharp](https://www.nuget.org/packages/HtmlRenderer.PdfSharp) (2认同)

Je *_*not 8

不,PDFsharp目前不包含解析HTML文件的代码.


Roh*_*ora 5

老问题,但以上都不适合我。然后我尝试了HtmlRenderer结合pdfsharpgeneratepdf的方法。希望它有帮助:您必须安装一个名为.HtmlRenderer.pdfsharp

var doc = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf("Your html in a string",PageSize.A4);
  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(Server.MapPath("test.pdf"));
  doc.Close();
Run Code Online (Sandbox Code Playgroud)

  • 第 2-7 行中的所有代码都在做什么?您肯定只需要创建文档然后保存它吗? (2认同)

VDW*_*WWD 5

如果您只想将某个 HTML 字符串写入 PDF,而不是其余部分,则可以使用HtmlContainerTheArtOfDev HtmlRenderer。此代码片段使用 V 1.5.1

using PdfSharp.Pdf;
using PdfSharp;
using PdfSharp.Drawing;
using TheArtOfDev.HtmlRenderer.PdfSharp;

//create a pdf document
using (PdfDocument doc = new PdfDocument())
{
    doc.Info.Title = "StackOverflow Demo PDF";

    //add a page
    PdfPage page = doc.AddPage();
    page.Size = PageSize.A4;

    //fonts and styles
    XFont font = new XFont("Arial", 10, XFontStyle.Regular);
    XSolidBrush brush = new XSolidBrush(XColor.FromArgb(0, 0, 0));

    using (XGraphics gfx = XGraphics.FromPdfPage(page))
    {
        //write a normal string
        gfx.DrawString("A normal string written to the PDF.", font, brush, new XRect(15, 15, page.Width, page.Height), XStringFormats.TopLeft);

        //write the html string to the pdf
        using (var container = new HtmlContainer())
        {
            var pageSize = new XSize(page.Width, page.Height);

            container.Location = new XPoint(15,  45);
            container.MaxSize = pageSize;
            container.PageSize = pageSize;
            container.SetHtml("This is a <b>HTML</b> string <u>written</u> to the <font color=\"red\">PDF</font>.<br><br><a href=\"http://www.google.nl\">www.google.nl</a>");

            using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
            {
                container.PerformLayout(measure);
            }

            gfx.IntersectClip(new XRect(0, 0, page.Width, page.Height));

            container.PerformPaint(gfx);
        }
    }

    //write the pdf to a byte array to serve as download, attach to an email etc.
    byte[] bin;
    using (MemoryStream stream = new MemoryStream())
    {
        doc.Save(stream, false);
        bin = stream.ToArray();
    }
}
Run Code Online (Sandbox Code Playgroud)