添加图像到单元格- iTextSharp

本文关键字:iTextSharp 单元格 图像 添加 | 更新日期: 2023-09-27 18:08:19

我试图将图像添加到pdf单元格,我得到一个错误:

参数1:不能从'System.Drawing. '转换。图像的"iTextSharp.text.pdf.PdfPCell"

:

content.AddCell(myImage);

下面是我的代码:

PdfPTable content = new PdfPTable(new float[] { 500f });
content.AddCell(myImage);
document.Add(content);

myImage变量的类型为Image。我做错了什么?

添加图像到单元格- iTextSharp

你不能只是添加一个图像,你需要先创建单元格,并将图像添加到单元格:http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html PdfPCell (com.itextpdf.text.Image)

PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);

你也不能使用System.Drawing.Image类,iTextSharp有自己的Image类:http://api.itextpdf.com/itext/com/itextpdf/text/Image.html

需要一个URL传递给构造函数到图像的位置。

:

iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance("Image location");
PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);

您应该首先创建一个单元格,然后将图像添加到该单元格中,最后将单元格添加到表格中。

var image = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
var imageCell = new PdfPCell(image);
content.AddCell(imageCell);
如何在iTextSharp中使用webmatrix为表格单元格添加图像