itext7 -- System.TypeInitializationException:“iText.IO.Util.ResourceUtil”的类型初始值设定项引发异常

问题描述 投票:0回答:2

我在 Xamarin 表单应用程序(Android 和 IOS)的 itext PDF 生成器中尝试了 POC,

检查场景: itext Sharp 版本:5.5.21 - 在 PCL 项目和本机项目中都运行良好。 itext7 版本:7.1.0 - 出现以下错误

System.TypeInitializationException: The type initializer for 
'iText.IO.Util.ResourceUtil' threw an exception. ---> 
System.TypeInitializationException: The type initializer for 
'Microsoft.Extensions.DependencyModel.DependencyContextLoader' threw an 
exception. ---> System.TypeInitializationException: The type initializer for
 'Microsoft.Extensions.DependencyModel.DependencyContextPaths' threw an 
exception. ---> System.NotImplementedException: The method or operation is not 
implemented.

在论坛中据说这个问题已在之前的版本中修复。我之前需要初始化的任何东西。附上下面尝试的代码,

PdfDocument pdf = new PdfDocument(new PdfWriter(fs, new WriterProperties().AddXmpMetadata()));
Document document = new Document(pdf);

pdf.SetTagged();
pdf.GetCatalog().SetLang(new PdfString("en-US"));
pdf.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
PdfDocumentInfo info = pdf.GetDocumentInfo();
info.SetTitle("iText7 PDF/UA example");

PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.SetFont(font);
 p.Add(new Text("The quick brown "));

document.Add(p);
document.Close();

当断点命中 PdfFont 时,它会引发上述错误。

xamarin.ios xamarin.forms xamarin.android itext itext7
2个回答
1
投票

您的代码适用于 iText 5,但不适用于 iText 7。这是正常的。 iText 7 是 iText 5 的完全重写(从头开始!)。API 不向后兼容,因此如果您有 iText 5 代码,如果您希望该代码适用于 iText 7,则必须重写它(从头开始!) .

您会遇到所有这些异常,因为所有这些方法(甚至其中许多类)在 iText 7 中不再存在;它们已被其他方法(和类)取代。

本教程将为您提供快速入门:iText 7:.NET 快速入门教程


0
投票

对于 WPF 应用程序,itext7 (2023) 遇到了同样的问题,但也许它也可以在这里提供帮助,我清除了我的构建存储库,然后重建了我的代码,它工作正常。所以我建议你也尝试一下,根据你使用的版本清除 /bin/debug 或 /release 并尝试一下。

© www.soinside.com 2019 - 2024. All rights reserved.