0

我正在使用 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();
4

2 回答 2

0

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>
于 2016-06-22T08:57:51.017 回答
0

它在 gsp 页面中对我有用。希望能帮助到你

<div align="center">
    <p>
        <img src="http://www.uaa.alaska.edu/institutionaleffectiveness/Graduation/images/report_1.jpg" width="120">
    </p>
</div>
于 2020-03-27T13:14:23.527 回答