【问题标题】:itext7 -- System.TypeInitializationException: The type initializer for 'iText.IO.Util.ResourceUtil' threw an exceptionitext7 -- System.TypeInitializationException:“iText.IO.Util.ResourceUtil”的类型初始化程序引发异常
【发布时间】:2017-12-11 12:33:42
【问题描述】:

我在 itext PDF 生成器中为 Xamarin 表单应用程序(Android 和 IOS)尝试了 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


    【解决方案1】:

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

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

    本教程将为您提供快速入门:iText 7: Jump-start tutorial for .NET

    【讨论】:

    • 我已经从 nuget 安装了 itext7。当作为控制台应用程序执行时,相同的代码工作正常,但在 Xamarin 表单和 PCL 中不起作用。
    猜你喜欢
    • 2018-09-01
    • 2017-08-17
    • 1970-01-01
    • 2016-11-30
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多