简体   繁体   English

使用 itextsharp 转换为 PDF 时 HTML 中的图像未正确对齐

[英]Image in HTML not getting aligned properly on converting to PDF using itextsharp

I'm using itextsharp to convert my html portion to pdf.我正在使用 itextsharp 将我的 html 部分转换为 pdf。 Everything is ok but the image always getting aligned left after converting html to pdf一切正常,但在将 html 转换为 pdf 后,图像总是向左对齐

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

C# code: 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();

ITextSharp does not support Div tag so make use of table ITextSharp 不支持 Div 标签,所以使用 table

<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>

It works for me in gsp pages.它在 gsp 页面中对我有用。 Hope it helps希望能帮助到你

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM