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

在.NET MAUI中使用iText7时如何解决Unknown PdfException?

解决.NET MAUI中iText7生成PDF的NotSupportedException异常

问题描述

在.NET MAUI移动端应用中使用iText7生成PDF时,执行var writer = new PdfWriter(stream);代码行出现以下异常:

Unknown PdfException. ---> System.NotSupportedException:Either itext7.bouncy-castle-adapter or itext7.bouncy-castle-fips-adap...

用户的生成PDF代码如下:

public static MemoryStream CreatePdf()
{
    try
    {
        using (var stream = new MemoryStream())
        {
            var writer = new PdfWriter(stream);
            var pdf = new PdfDocument(writer);
            var document = new Document(pdf);
            var paragraph = new Paragraph("hello");
            document.Add(paragraph);
            document.Close();
            stream.Position = 0;
            return stream;
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        return null;
    }
}

解决方法

这个异常的核心原因是iText7在跨平台环境(如.NET MAUI)下需要依赖BouncyCastle适配器处理加密、哈希等底层操作,而项目中缺少对应的依赖包。

  • 步骤1:安装适配NuGet包
    根据环境需求选择安装对应的包:

    • 普通非FIPS合规环境:安装itext7.bouncy-castle-adapter
    • FIPS合规环境:安装itext7.bouncy-castle-fips-adapter
      注意:必须保证适配器包版本与项目中iText7主包(itext7)的版本完全一致,避免版本兼容问题。
  • 步骤2:验证运行
    安装完成后,清理并重新构建.NET MAUI项目,再次执行生成PDF的代码即可正常运行。

内容的提问来源于stack exchange,提问作者Conrad Budde

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 12:17:40