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

标签 asp.net razor itextsharp webmatrix

我制作了一个包含单元格的表格,并且有兴趣在其中一个单元格中放置图像。 下面是我的代码:

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);

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

最佳答案

在最基本的层面上,您可以简单地将图像添加到 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);

更新

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

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

I.E

Server.MapPath(imagepath) + "\\logo.jpg"

关于asp.net - 如何使用 webmatrix 将图像添加到 iTextSharp 中的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589044/

相关文章:

asp.net - 即使在 applicationHost.config 中允许,gzip 压缩也不起作用

razor - 为什么 Html Helper 为 View 模型子项的属性添加前缀?

c# - iTextSharp 打印 gridview

JQuery ASP.NET .change 事件未触发

asp.net - 将代码部署到服务器后,样式表的工作方式有所不同

c# - LINQ 查询中的 "=>"标志是什么?

javascript - 如何在 Javascript 函数中迭代 @Model.ICollection?

asp.net-mvc - CSS Select List 那个 'drops' 到右边

c# - 使用 iTextSharp 验证 PDF 是否受到保护/保护

c# - 编辑 iTextSharp PdfSmartCopy 类的 DirectContent