Michael Lutz
Michael Lutz

Reputation: 43

Error when calling PdfFontFactory.CreateFont, System.NotSupportedException: The invoked member is not supported in a dynamic assembly in c# WebApp

When calling

PdfFontFactory.CreateFont(FontConstants.HELVETICA);

or

PdfFontFactory.CreateFont();

inside a web application, target framework 4.0, I get following error.

[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

I thought I could work around the problem only using

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

but that will result only in a blank PDF without an error though.

When I run the same code in a c# console app I get a valid PDF with all the fonts. Also the c# console app targets .net 4, so I am quite positive that it has nothing to do with the target framework. Thanks for any feedback.

Upvotes: 3

Views: 8017

Answers (3)

Michael Lutz
Michael Lutz

Reputation: 43

There is a bug in C# version of the library. Please see this pull requests for details how to fix it locally, or wait for a fix from iText team: github.com/itext/itext7-dotnet/pull/2 – Alexey Subach Mar 9 at 18:20

Upvotes: 1

Alexey Subach
Alexey Subach

Reputation: 12312

iText team has released a special .NET version 7.0.2.2 with the fix for the described problem. This version is basically just the 7.0.2 version with the hotfix. There will be no 7.0.2.2 for Java because the problem only occurred in .NET in some circumstances.

New 7.0.2.2 version can be downloaded from NuGet or from iText Artifactory.

Upvotes: 1

Alexey Obukhov
Alexey Obukhov

Reputation: 854

For iTextSharp:

  1. Download your favorite font as *.ttf file
  2. Create Font object from file:

    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. Use this object when add elements to document:

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

For iText7 look this example. Format is very similar, but use another class to create font:

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

Upvotes: 3

Related Questions