Cie*_*eja 4 .net c# pdf asp.net-mvc itext
我正在尝试将图像添加到 pdf 单元格,但出现错误:
参数 1:无法从“System.Drawing.Image”转换为“iTextSharp.text.pdf.PdfPCell”
排队:
content.AddCell(myImage);
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
PdfPTable content = new PdfPTable(new float[] { 500f });
content.AddCell(myImage);
document.Add(content);
Run Code Online (Sandbox Code Playgroud)
myImage变量是图像类型。我究竟做错了什么?
您不能只添加图像,您需要先创建单元格并将图像添加到单元格:http : //api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html#PdfPCell( com.itextpdf.text.Image)
PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);
Run Code Online (Sandbox Code Playgroud)
你也不能使用 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);
Run Code Online (Sandbox Code Playgroud)