如何使用webmatrix将图像添加到iTextSharp中的表格单元格中

Sit*_*nya 1 asp.net itextsharp webmatrix razor

我已经制作了一个带有单元格的表,并且有兴趣在其中一个单元格中创建一个图像.以下是我的代码:

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);
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,图像显示在我的pdf中,但我希望它在一个单元格中,以便我可以在图像的右侧添加更多单元格.

Dar*_*ght 10

在最基本的层面上,您可以简单地将图像添加到PdfPCell并将此单元格添加到您的表中.

所以使用你的代码......

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);
Run Code Online (Sandbox Code Playgroud)

更新

检查你的imagepath.这应该是图像的绝对路径,而不是网站页面中的相对路径.另外,将`/logo.jpg'更改为'\ logo.jpg'

这是假设imagepath实际上是目录,而不是实际的图像......

IE

Server.MapPath(imagepath) + "\\logo.jpg"
Run Code Online (Sandbox Code Playgroud)