mskuratowski
mskuratowski

Reputation: 4124

Add image to cell - iTextSharp

I'm trying to add an image to a pdf cell and I get an error:

Argument 1: cannot convert from 'System.Drawing.Image' to 'iTextSharp.text.pdf.PdfPCell'

in line:

content.AddCell(myImage);

Here's my code:

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

myImage variable is of the Image type. What am I doing wrong?

Upvotes: 4

Views: 18565

Answers (2)

Draken
Draken

Reputation: 3189

You can't just add an image, you need to create the cell first and add the image to the cell: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html#PdfPCell(com.itextpdf.text.Image)

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

You also cannot use the System.Drawing.Image class, iTextSharp has it's own class of Image: http://api.itextpdf.com/itext/com/itextpdf/text/Image.html

It needs a URL passed to the constructor to the image location.

So:

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

Upvotes: 7

janhartmann
janhartmann

Reputation: 15003

You should create a cell first, then add the image to that cell and finally add the cell the the table.

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

See answer on this post: How to add an image to a table cell in iTextSharp using webmatrix

Upvotes: 2

Related Questions