【问题标题】:Image in HTML not getting aligned properly on converting to PDF using itextsharp使用 itextsharp 转换为 PDF 时 HTML 中的图像未正确对齐
【发布时间】:2014-02-10 08:50:37
【问题描述】:

我正在使用 itextsharp 将我的 html 部分转换为 pdf。一切正常,但在将 html 转换为 pdf 后图像总是左对齐

<div align="center"><img src="http://www.uaa.alaska.edu/institutionaleffectiveness/Graduation/images/report_1.jpg" width="120"></div>

C#代码:

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlCertificate.RenderControl(hw);
src = sw.ToString();
AbsolutePath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
src = src.Replace("src=\"/", string.Format("src=\"{0}", AbsolutePath));
StringReader sr = new StringReader(src);
Document pdfDoc = new Document();
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, new FileStream(path + "/" + _CertificatesEntityCollection.First().Name + ".pdf", FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();

【问题讨论】:

  • HTMLWorker 已经被弃用了一段时间。请使用 XMLWorker:sourceforge.net/projects/xmlworker
  • 我尝试使用 XMLWorker,但没有帮助。我的图像仍然向左对齐。最后我找到了 GemBox Document。它运作良好。但是,他们只允许免费转换 20 段,这对我的项目来说已经绰绰有余了。

标签: c# asp.net itextsharp html-to-pdf


【解决方案1】:

ITextSharp 不支持 Div 标签,所以使用表格

<table width="100%">
    <tr>
        <td align="center">
            <img src="http://www.uaa.alaska.edu/institutionaleffectiveness/Graduation/images/report_1.jpg" width="120">
        </td>
    </tr>
</table>

【讨论】:

    【解决方案2】:

    它适用于 gsp 页面。希望对你有帮助

    <div align="center">
        <p>
            <img src="http://www.uaa.alaska.edu/institutionaleffectiveness/Graduation/images/report_1.jpg" width="120">
        </p>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-03-07
      • 2010-10-17
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多