0

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();
Ramesh Vaasu
  • 86
  • 1
  • 8
  • 1
    HTMLWorker has been deprecated for quite some time now. Please use XMLWorker: http://sourceforge.net/projects/xmlworker/ – Michaël Demey Feb 10 '14 at 09:17
  • I tried using XMLWorker but it didnt help. Still my image was aligning left. Finally I found GemBox Document. It works good. But, they will allow only 20 paragraphs of conversion free of cost, it was more than enough for my project. – Ramesh Vaasu Mar 08 '14 at 10:14

2 Answers2

0

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>
Hardipsinh Jadeja
  • 1,180
  • 1
  • 13
  • 30
0

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>
Viplav Soni
  • 1,489
  • 13
  • 18