4

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?

Draken
  • 3,134
  • 13
  • 34
  • 54
mskuratowski
  • 4,014
  • 15
  • 58
  • 109
  • 1
    Without knowing the API you probably need to create a cell and put the image there and then use `content.AddCell(yourCreatedCell);` – janhartmann Aug 17 '15 at 08:23

2 Answers2

7

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);
Draken
  • 3,134
  • 13
  • 34
  • 54
  • Now I got an error: Argument 1: cannot convert from 'System.Drawing.Image' to 'iTextSharp.text.Phrase' – mskuratowski Aug 17 '15 at 08:27
  • Apoliges, see details above, you need to use iTextSharps own Image function found here: http://api.itextpdf.com/itext/com/itextpdf/text/Image.html – Draken Aug 17 '15 at 08:28
2

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

Community
  • 1
  • 1
janhartmann
  • 14,713
  • 15
  • 82
  • 138