如何使用WebMatrix的图像添加到一个表格单元格中iTextSharp的 [英] How to add an image to a table cell in iTextSharp using webmatrix
问题描述
我已经作出了表细胞和有意在细胞中的一个具有的图像。
下面是我的code:
I have made a table with cells and interested in having an image in one of the cell. 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);
通过上面的code,图像显示在我的PDF,但我希望它是一个细胞内,这样我可以添加更多的细胞图像的右侧。
With the above code, the image shows 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.
推荐答案
在非常基本的水平,你可以将图像简单地添加到PdfPCell这种细胞添加到您的表。
On the very basic level you can simply add the image to a PdfPCell and add this cell to your table.
所以使用code ...
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);
更新
检查的ImagePath
。这应该是对图像的绝对路径,而不是相对的像在一个网站页面。另外,你的`/logo.jpg改变'到'\\ logo.jpg
Check your imagepath
. This should be an absolute path to the image, and not relative like in a website page. Also, change your `/logo.jpg' to '\logo.jpg'
这是假设的ImagePath
实际的目录,而不是实际的图像...
this is assuming imagepath
is actually to the directory, and not the actual image...
即
Server.MapPath(imagepath) + "\\logo.jpg"
这篇关于如何使用WebMatrix的图像添加到一个表格单元格中iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!