2014-02-10 31 views
0

我正在使用itextsharp将我的html部分转换为pdf。一切都OK,但图像始终得到转换HTML到PDF使用itextsharp转换为PDF时,HTML中的图像无法正确对齐

<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(); 
+1

HTMLWorker已经被相当一段时间弃用了。请使用XMLWorker:http://sourceforge.net/projects/xmlworker/ –

+0

我尝试使用XMLWorker,但它没有帮助。我的形象仍然是左对齐。最后我找到了GemBox Document。它运作良好。但是,他们只允许20段免费的转换,这对我的项目来说已经足够了。 –

回答

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> 
相关问题