2014-02-10 30 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> 
相關問題