【问题标题】:Error when calling PdfFontFactory.CreateFont, System.NotSupportedException: The invoked member is not supported in a dynamic assembly in c# WebApp调用 PdfFontFactory.CreateFont 时出错,System.NotSupportedException:c# WebApp 的动态程序集中不支持调用的成员
【发布时间】:2017-03-09 15:36:03
【问题描述】:

打电话时

PdfFontFactory.CreateFont(FontConstants.HELVETICA);

或

PdfFontFactory.CreateFont();

在 Web 应用程序中,目标框架 4.0,我收到以下错误。

[NotSupportedException: The invoked member is not supported in a dynamic assembly.]
   System.Reflection.Emit.InternalAssemblyBuilder.get_Location() +52
   iText.IO.Util.ResourceUtil.<LoadITextResourceAssemblies>b__3(Assembly a) +30
   System.Linq.WhereSelectListIterator`2.MoveNext() +115
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +239
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +77
   iText.IO.Util.ResourceUtil.LoadITextResourceAssemblies() +172
   iText.IO.Util.ResourceUtil..cctor() +125

[TypeInitializationException: The type initializer for 'iText.IO.Util.ResourceUtil' threw an exception.]
   iText.IO.Font.Type1Parser.GetMetricsFile() +127
   iText.IO.Font.Type1Font.Process() +53
   iText.IO.Font.Type1Font..ctor(String metricsPath, String binaryPath, Byte[] afm, Byte[] pfb) +131
   iText.IO.Font.FontProgramFactory.CreateFont(String name, Byte[] fontProgram, Boolean cached) +381
   iText.Kernel.Font.PdfFontFactory.CreateFont(String fontProgram, String encoding) +29
   iText.Kernel.Font.PdfFontFactory.CreateFont(String fontProgram) +31
   PdfCreator.x(String pdf_file_name) in x.cs:165
   ASP.x_cshtml.Execute() in x.cshtml:40
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +196
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +151
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +114

我认为我只能使用来解决这个问题

PdfFontFactory.Register(windows_fonts + "ARIAL.TTF", "Arial");
PdfFont Arial = PdfFontFactory.CreateRegisteredFont("Arial");

但这只会导致一个没有错误的空白 PDF。

当我在 c# 控制台应用程序中运行相同的代码时,我会得到一个包含所有字体的有效 PDF。 此外,c# 控制台应用程序以 .net 4 为目标,因此我非常肯定它与目标框架无关。感谢您的任何反馈。

【问题讨论】:

  • 库的 C# 版本中存在错误。有关如何在本地修复它的详细信息,请参阅此拉取请求,或等待 iText 团队的修复:github.com/itext/itext7-dotnet/pull/2
  • 谢谢,现在我明白我的问题了。

标签: c# .net asp.net-webpages itext7


【解决方案1】:

iText 团队发布了一个特殊的.NET 版本7.0.2.2,并修复了所描述的问题。这个版本基本上只是带有修补程序的7.0.2 版本。 Java 不会有7.0.2.2,因为在某些情况下问题只出现在.NET。

新的7.0.2.2版本可以从NuGet或iText Artifactory下载。

【讨论】:

    【解决方案2】:

    对于 iTextSharp:

    1. 将您喜欢的字体下载为 *.ttf 文件
    2. 从文件创建字体对象:

      string path = Path.Combine(AppDataPath, "helvetica.ttf");
      BaseFont baseFont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
      Font font = new Font(baseFont, fontSize, Font.NORMAL);
      
    3. 向文档添加元素时使用此对象:

      var p = new Paragraph("hello there!", font);
      doc.Add(p);
      

    对于 iText7,请查看 example。格式很相似,但是使用另一个类来创建字体:

       PdfFont f1 = PdfFontFactory.createFont(ttf_file_path, "Cp1250", true);
       Paragraph p1 = new Paragraph("Testing of letters").setFont(f1);
       doc.add(p1);
    

    【讨论】:

    • 感谢您在 iText5 语法中发布的快速回复,这是否意味着在使用 c# 网页时应该坚持使用 iText5 而不是 iText7?
    • 我使用 iTextSharp (github.com/itext/itextsharp),iText 是同一公司的另一个库 (github.com/itext/itext7-dotnet)。我没有使用 iText 的经验。
    • 您的代码不会引发异常,但生成的 pdf 没有字符。我只是无法弄清楚为什么每个函数都可以与 PdfFontFactory.CreateFont();我想一旦该功能起作用,我将能够使其他字体也起作用。
    【解决方案3】:

    库的 C# 版本中存在错误。有关如何在本地修复它的详细信息,请参阅此拉取请求,或等待 iText 团队的修复:github.com/itext/itext7-dotnet/pull/2 – Alexey Subach 3 月 9 日 18:20

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2016-02-20
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多