You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iTextSharp在C#生成PDF时遭遇BMP版本5未实现异常

解决iTextSharp加载BMP版本5图片的异常问题

问题描述

我在C#环境下使用iTextSharp生成PDF报表,已导入相关库并编写了从DataTable生成PDF的方法,但在加载BMP格式的logo图片时抛出异常,提示不支持BMP版本5。

相关代码

导入的库及logo路径定义

using iText = iTextSharp.text;
using iTextSharp.text.pdf;

private readonly string _logo = HttpContext.Current.Server.MapPath("img/report.bmp");

PDF生成方法片段

public string WritePDFReport(DataTable dataTable)
{
    try
    {
        if (true)//ReportCount() > 0
        {
            var rptUrl = Guid.NewGuid().ToString().ToUpper().Replace("-", "") + ".pdf";
            var rpt = Path.Combine(_rptPath, rptUrl);
            var fs = new FileStream(rpt, FileMode.Create);
            var doc = new iText.Document(iTextSharp.text.PageSize.LETTER_LANDSCAPE, 10, 10, 10, 10);
            var writer = PdfWriter.GetInstance(doc, fs);
            var p = new iText.Phrase("Date: " + DateTime.Now.ToString("yyyy MMM dd hh:mm:ss tt zzz"),
                                     new iText.Font(iText.Font.FontFamily.HELVETICA, 6, iText.Font.BOLDITALIC));
            doc.Open();
            var page = doc.PageSize;
            var h = new PdfPTable(1) { TotalWidth = page.Width };
            var c = new PdfPCell(p)
            {
                Border = iTextSharp.text.Rectangle.NO_BORDER,
                VerticalAlignment = iText.Element.ALIGN_TOP,
                HorizontalAlignment = iText.Element.ALIGN_RIGHT
            };
            h.AddCell(c);
            doc.Add(h);
            var th = new PdfPTable(4);
            var logo = iText.Image.GetInstance(_logo);
            //logo.ScaleAbsolute(100, 97);
            logo.ScaleAbsolute(79, 74);
            c = new PdfPCell(logo) { Colspan = 1, Rowspan = 3, Border = iTextSharp.text.Rectangle.NO_BORDER };
            th.AddCell(c);
            // 后续遍历DataTable写入数据的代码
        }
        // 方法返回逻辑
    }
    catch (Exception ex)
    {
        // 异常处理
    }
}

抛出的异常信息

System.Exception: BMP version 5 not implemented yet.
   at iTextSharp.text.pdf.codec.BmpImage.Process(Stream stream, Boolean noHeader)
   at iTextSharp.text.pdf.codec.BmpImage..ctor(Stream isp, Boolean noHeader, Int32 size)
   at iTextSharp.text.pdf.codec.BmpImage.GetImage(Stream isp, Boolean noHeader, Int32 size)
   at iTextSharp.text.pdf.codec.BmpImage.GetImage(Uri url)
   at iTextSharp.text.Image.GetInstance(Uri url)
   at iTextSharp.text.Image.GetInstance(String filename)
   at ReportEngine.WritePDFReport(DataTable dataTable)

当前使用的iTextSharp版本

itext dll details

解决方案

以下几种方式可以解决该问题:

  • 转换BMP图片版本:使用系统自带的画图工具或专业图像编辑软件打开当前的BMP图片,选择另存为旧版本的BMP格式(如BMP版本3或4),替换原图片后重新运行代码。

  • 更换图片格式:将BMP格式的logo替换为PNG、JPG等iTextSharp完全支持的图片格式,同时修改代码中_logo变量的路径后缀,确保指向新格式的图片文件。

  • 通过System.Drawing中转处理图片:在代码中先使用System.Drawing加载BMP图片,再转换为iTextSharp支持的格式(如PNG)后传入,示例代码如下:

// 替换原logo加载代码段
using (var bmp = new Bitmap(_logo))
{
    using (var ms = new MemoryStream())
    {
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        ms.Position = 0;
        var logo = iText.Image.GetInstance(ms);
        logo.ScaleAbsolute(79, 74);
        var c = new PdfPCell(logo) { Colspan = 1, Rowspan = 3, Border = iTextSharp.text.Rectangle.NO_BORDER };
        th.AddCell(c);
    }
}
  • 考虑迁移到iText 7:iTextSharp已停止维护,iText 7对图片格式的支持更完善,若项目有条件,建议迁移到最新的iText 7框架。

内容的提问来源于stack exchange,提问作者Timothy Odhiambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 05:04:58