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

如何正确使用[iText.Kernel.Pdf.PdfDocument]::new()读取PDF并解决初始化异常?

问题:PowerShell中使用iText 8初始化PdfDocument时出现类型初始化异常

已成功加载iText组件并解决此前的文件找不到错误,执行以下代码无报错:

Add-Type -path 'C:/itext_2/itext.8.0.2/lib/netstandard2.0/itext.kernel.dll'
Add-Type -path 'C:\itext_2\itext.commons.8.0.2\lib\netstandard2.0\itext.commons.dll'
Add-Type -path 'C:/itext_2/itext.8.0.2/lib/netstandard2.0/itext.io.dll'

Add-Type -path 'C:/itext_2/common.logging.3.4.1/lib/netstandard1.3/Common.Logging.dll'
Add-Type -path 'C:/itext_2/common.logging.core.3.4.1/lib/netstandard1.0/Common.Logging.Core.dll'

$reader = [iText.Kernel.Pdf.PdfReader]::new("C:/itext_2/hello-world.pdf")

运行$reader返回iText.Kernel.Pdf.PdfReader,通过Get-Member可查看其成员方法。根据官方文档,需将该$reader对象传入[iText.Kernel.Pdf.PdfDocument]::new()构造函数,但执行以下代码时:

$PdfDoc = [iText.Kernel.Pdf.PdfDocument]::new($reader)

出现如下错误:

MethodInvocationException:
Line |
   9 |  $PdfDoc = [iText.Kernel.Pdf.PdfDocument]::new($reader)
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "1" argument(s): "The type initializer for 'iText.Commons.Actions.EventManager' threw an exception."

测试用PDF文件已准备就绪,由于iText是iTextSharp的升级版本,多数示例为iTextSharp相关,且官方文档为C#语言(本人不懂C#),难以自行解决问题。现求助如何在PowerShell中读取PDF文档,或解决上述初始化异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:06:16