【问题标题】:How to add an image to a table cell in iTextSharp using webmatrix如何使用 webmatrix 将图像添加到 iTextSharp 中的表格单元格
【发布时间】:2013-05-16 13:39:25
【问题描述】:

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

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 中,但我希望它位于一个单元格内,以便我可以在图像右侧添加更多单元格。

【问题讨论】:

    标签: asp.net razor itextsharp webmatrix


    【解决方案1】:

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

    更新

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

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

    I.E

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

    【讨论】:

    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2011-07-09
    • 2011-09-10
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多