调用PdfonFactory.CreateFont、System.NotSupportedException时出错:c#WebApp中的动态程序集中不支持调用的成员

调用PdfonFactory.CreateFont、System.NotSupportedException时出错:c#WebApp中的动态程序集中不支持调用的成员,c#,.net,asp.net-webpages,itext7,C#,.net,Asp.net Webpages,Itext7,打电话的时候 PdfFontFactory.CreateFont(FontConstants.HELVETICA); 或 在web应用程序TargetFramework4.0中,我遇到以下错误 [NotSupportedException: The invoked member is not supported in a dynamic assembly.] System.Reflection.Emit.InternalAssemblyBuilder.get_Location() +52

打电话的时候

PdfFontFactory.CreateFont(FontConstants.HELVETICA);

在web应用程序TargetFramework4.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
但这只会导致一个没有错误的空白PDF

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

对于iTextSharp:

  • 下载您喜爱的字体作为*.ttf文件
  • 从文件创建字体对象:

    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);
    
  • 将元素添加到文档时使用此对象:

    var p = new Paragraph("hello there!", font);
    doc.Add(p);
    
  • 对于iText7,请看这个。格式非常相似,但使用另一个类创建字体:

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

    库的C#版本中有一个bug。请参阅此拉取请求,了解如何在本地修复它,或等待iText团队的修复:Alexey Subach 3月9日18:20,iText团队发布了一个特殊的
    .NET
    版本
    7.0.2.2
    ,并针对所述问题进行了修复。这个版本基本上就是带有修补程序的
    7.0.2
    版本。Java将不会出现
    7.0.2.2
    ,因为在某些情况下问题只发生在
    .NET


    新的
    7.0.2.2
    版本可以从中或从中下载。

    感谢您的快速回复,因为您发布的是iText5语法,这是否意味着在使用c#网页时应该坚持使用iText5而不是iText7?我使用iTextSharp(),iText是同一公司的另一个库()。我没有使用iText的经验。您的代码不会引发异常,但生成的pdf没有字符。我只是不明白为什么除了PdfFontFactory.CreateFont()之外,每个函数都可以工作;我想一旦这个函数起作用,我就可以让其他字体也起作用了。这个库的C版本中有一个bug。请参阅此pull请求,了解如何在本地修复它的详细信息,或者等待iText团队的修复:谢谢,现在我了解了我的问题。
    var p = new Paragraph("hello there!", font);
    doc.Add(p);
    
       PdfFont f1 = PdfFontFactory.createFont(ttf_file_path, "Cp1250", true);
       Paragraph p1 = new Paragraph("Testing of letters").setFont(f1);
       doc.add(p1);