How to add image to table cell in iTextSharp using webmatrix
I made a table with cells and became interested in the presence of an image in one of the cells. Below is my code:
doc.Open();
PdfPTable table = new PdfPTable(2);
table.TotalWidth = 570f;
table.LockedWidth = true;
table.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(points);
// add a image
doc.Add(table);
Image jpg = Image.GetInstance(imagepath + "/logo.jpg");
doc.Add(jpg);
With the above code, the image is displayed in my pdf, but I want it to be inside a cell so that I can add more cells to the right of the image.
0
source to share
1 answer
At the most basic level, you can simply add an image to PdfPCell and add that cell to your table.
So using your code ...
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
// add a image
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
PdfPCell imageCell = new PdfPCell(jpg);
imageCell.Colspan = 2; // either 1 if you need to insert one cell
imageCell.Border = 0;
imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.AddCell(points);
// add a image
table.AddCell(imageCell);
doc.Add(table);
Update
Check it out imagepath
. This must be an absolute path to the image, not a relative path like on a website page. Also, change your `/logo.jpg 'to' \ logo.jpg '
this assumes it imagepath
is actually a directory and not an actual image ...
i.e
Server.MapPath(imagepath) + "\\logo.jpg"
+7
source to share