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?
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With