iText7更新后实例化PdfDocument遇System.TypeInitializationException求助
问题:更新iText7后创建PdfDocument时抛出TypeInitializationException异常
最近更新iText7后,原本正常运行的代码在创建PdfDocument实例时抛出System.TypeInitializationException异常。最内层异常为FileNotFoundException,提示无法加载程序集Microsoft.Extensions.Logging, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60或其依赖项,系统找不到指定文件。
已确认项目引用了Microsoft.Extensions.Logging 5.0.0版本的包,且文件确实存在。代码运行在基于.NET Framework 4.8的Windows Forms应用中,D:\temp为有权限访问的有效文件夹。
报错代码片段
public static void CreatePDF(InvoiceModel model, string outFilePath) { using (PdfWriter writer = new PdfWriter(outFilePath)) { PdfDocument pdf = new PdfDocument(writer); Document document = new Document(pdf);
outFilePath 值
outFilePath "d:\\temp\\Statement-WTCC-20221117.pdf" string
完整异常信息
System.TypeInitializationException HResult=0x80131534 Message=The type initializer for 'iText.Commons.Actions.EventManager' threw an exception. Source=itext.kernel StackTrace: at iText.Kernel.Pdf.PdfDocument.Open(PdfVersion newPdfVersion) at LabBilling.Core.InvoicePrint.CreatePDF(InvoiceModel model, String outFilePath) in D:\users\bpowers\source\repos\Lab-Billing\LabBilling Library\BusinessLogic\InvoicePrint.cs:line 33 at LabBilling.Core.ClientInvoices.GenerateStatement(String clientMnemonic, DateTime asOfDate) in D:\users\bpowers\source\repos\Lab-Billing\LabBilling Library\BusinessLogic\ClientInvoices.cs:line 108 at LabBilling.Forms.ClientInvoiceForm.generateStatementButton_Click(Object sender, EventArgs e) in D:\users\bpowers\source\repos\Lab-Billing\Lab Billing UI\Forms\ClientInvoiceForm.cs:line 413 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) Inner Exception 1: TypeInitializationException: The type initializer for 'iText.Commons.Actions.ProductEventHandler' threw an exception. Inner Exception 2: TypeInitializationException: The type initializer for 'iText.Commons.ITextLogManager' threw an exception. Inner Exception 3: FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified. Inner Exception 4: FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Logging, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
解决思路
- 添加程序集绑定重定向:从异常链可见,iText7尝试加载6.0.0.0版本的日志组件,但项目引用的是5.0.0.0。在
App.config中添加绑定规则,强制框架使用已安装的版本:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Extensions.Logging" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> - 清理重建项目:删除项目的
bin和obj目录,重新生成解决方案,确保所有依赖包正确复制到输出文件夹。 - 检查依赖完整性:确认
Microsoft.Extensions.Logging的间接依赖(如Microsoft.Extensions.DependencyInjection.Abstractions)是否已正确安装,版本需与5.0.0匹配,避免缺失依赖项。 - 调整版本兼容性:尝试降级iText7到之前可用的版本,或者升级
Microsoft.Extensions.Logging到6.0.0版本(需确认该版本与.NET Framework 4.8兼容)。 - 验证输出文件:检查项目输出目录中是否存在
Microsoft.Extensions.Logging.dll,且版本为5.0.0.0;若缺失,检查NuGet包的“复制本地”属性是否设为True。
内容的提问来源于stack exchange,提问作者BradP
相关产品推荐
相关产品推荐

