简体   繁体   中英

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

I'm using itextsharp to convert my html portion to pdf. Everything is ok but the image always getting aligned left after converting html to pdf

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

C# code:

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

<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. Hope it helps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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