将图像添加到单元格-iTextSharp

问题描述 投票:2回答:2

我正在尝试将图像添加到pdf单元格,但出现错误:

参数1:无法从'System.Drawing.Image'转换为'iTextSharp.text.pdf.PdfPCell'

in line:

content.AddCell(myImage);

这是我的代码:

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

myImage变量是图像类型。我在做什么错?

c# .net asp.net-mvc pdf itext
2个回答
4
投票

您不仅可以添加图像,还需要先创建单元格并将图像添加到单元格中: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);

1
投票

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

var image = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
var imageCell = new PdfPCell(image);
content.AddCell(imageCell);

参见此帖子的答案:How to add an image to a table cell in iTextSharp using webmatrix

© www.soinside.com 2019 - 2024. All rights reserved.