2013-08-20 63 views
-1

我在用戶記錄數據庫中有一個表格,其中有一列Profile Pic,其中包含像~/images/user.JPEG這樣的圖像。我想將這些數據導出爲PDF。如何使用iText for .NET在PDF中插入圖像

我正在使用名爲iTextSharp的第三方庫,我想在用戶名前面顯示PDF文件中的圖像。我應該怎麼做?

我綁定網格視圖與數據庫,它顯示在網格視圖中,但是當我在PDF中導出它時,它顯示圖像的位置而不是圖像的位置。

這是我的代碼,它使用表和PdfPCell。

System.Web.UI.WebControls.Image image = (System.Web.UI.WebControls.Image)rows.FindControl("imgprofile"); 
string imagedummy = image.ImageUrl; 
PdfPCell c1 = new PdfPCell(new Phrase(id.Text, verdana)); 
PdfPCell c2 = new PdfPCell(new Phrase(firstname.Text, verdana)); 
PdfPCell c3 = new PdfPCell(new Phrase(lastname.Text, verdana)); 

PdfPCell c14 = new PdfPCell(new Phrase(image dummy)); 
t1.Add Cell(c14); 

回答

1

如果您有指向圖像的路徑,例如imgPath,您可以通過創建一個Image對象在PDF中使用該圖像。見我的書的第10章的例子:http://tinyurl.com/itextsharpIIA2C10

Image img = Image.GetInstance(imgPath); 

一旦有了這種Image對象,您可以創建與圖像參數PdfPCell,您可以將其添加到使用addElement()一個PdfPCell,你可以包裝內部圖像Chunk,...

所有這些不同的方法,將顯示不同的行爲。這一切都在the iText documentation解釋。