【问题标题】:Add image to cell - iTextSharp将图像添加到单元格 - iTextSharp
【发布时间】:2015-11-09 19:32:24
【问题描述】:

我正在尝试将图像添加到 pdf 单元格,但出现错误:

参数 1:无法从 'System.Drawing.Image' 转换为 'iTextSharp.text.pdf.PdfPCell'

排队:

content.AddCell(myImage);

这是我的代码:

PdfPTable content = new PdfPTable(new float[] { 500f });
content.AddCell(myImage);
document.Add(content);

myImage 变量属于 Image 类型。我做错了什么?

【问题讨论】:

  • 在不知道 API 的情况下,您可能需要创建一个单元格并将图像放在那里,然后使用 content.AddCell(yourCreatedCell);

标签: c# .net asp.net-mvc pdf itext


【解决方案1】:

不能只添加图片,需要先创建单元格,然后将图片添加到单元格中: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html#PdfPCell(com.itextpdf.text.Image)

PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);

您也不能使用 System.Drawing.Image 类,iTextSharp 有它自己的 Image 类: http://api.itextpdf.com/itext/com/itextpdf/text/Image.html

它需要一个 URL 传递给构造函数到图像位置。

所以:

iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance("Image location");
PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);

【讨论】:

【解决方案2】:

您应该先创建一个单元格,然后将图像添加到该单元格,最后将该单元格添加到表格中。

var image = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
var imageCell = new PdfPCell(image);
content.AddCell(imageCell);

查看此帖子的答案:How to add an image to a table cell in iTextSharp using webmatrix

【讨论】:

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