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

时间:2017-03-09 15:36:04

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

致电

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#console应用程序的目标是.net 4,所以我非常肯定它与目标框架无关。感谢您的任何反馈。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

iText团队发布了一个特殊的.NET7.0.2.2,其中包含针对所述问题的修复程序。此版本基本上只是带有修补程序的7.0.2版本。 Java没有7.0.2.2,因为在某些情况下问题只发生在.NET

可以从NuGetiText Artifactory下载新的7.0.2.2版本。

答案 2 :(得分:0)

对于iTextSharp:

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

    var p = new Paragraph("hello there!", font);
    doc.Add(p);
    
  3. 向文档添加元素时使用此对象:

       PdfFont f1 = PdfFontFactory.createFont(ttf_file_path, "Cp1250", true);
       Paragraph p1 = new Paragraph("Testing of letters").setFont(f1);
       doc.add(p1);
    
  4. 对于iText7,请查看此example。格式非常相似,但使用另一个类来创建字体:

    uitable