如何使用iText for .NET在PDF中插入图像

时间:2013-08-20 18:03:53

标签: c# webforms itext

我在用户记录数据库中有一个表,其中有一列Profile Pic,其中包含~/images/user.JPEG等图像。我想将此数据导出为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 个答案:

答案 0 :(得分:1)

如果您有图像的路径,例如imgPath,您可以通过创建Image对象在PDF中使用该图像。请参阅本书第10章的示例:http://tinyurl.com/itextsharpIIA2C10

Image img = Image.GetInstance(imgPath);

拥有此Image对象后,您可以使用该图片作为参数创建PdfPCell,您可以使用PdfPCell将其添加到addElement()Chunk内的图片,...

所有这些不同的方法,都会表现出不同的行为。这些都在the iText documentation中解释过。