Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 843
mskuratowski Avatar asked Jan 07 '23 18:01

mskuratowski


2 Answers

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);
like image 188
Draken Avatar answered Jan 12 '23 00:01

Draken


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

like image 25
janhartmann Avatar answered Jan 11 '23 22:01

janhartmann