2017-03-09 15 views
1

當調用錯誤:調用部件沒有在動態組件支撐在c#Web應用程序

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,所以我很積極,它與目標框架無關。感謝您的任何反饋。

+0

庫的C#版本存在一個錯誤。請查看此拉取請求以瞭解如何在本地修復此問題,或等待iText團隊修復:https://github.com/itext/itext7-dotnet/pull/2 –

+0

謝謝,現在我瞭解我的問題。 –

回答

1

iText團隊已經發布了一個特殊的.NET版本7.0.2.2,並解決了上述問題。該版本基本上只是帶有修補程序的7.0.2版本。對於Java,將不存在7.0.2.2,因爲在某些情況下,問題僅發生在.NET

7.0.2.2版本可以從NuGet或從iText Artifactory下載。

0

對於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); 
+0

感謝您在iText5語法中發佈後的快速回復,這是否意味着當使用c#網頁時,應該堅持iText5而不是iText7? –

+0

我使用iTextSharp(https://github.com/itext/itextsharp),iText是同一家公司的另一個庫(https://github.com/itext/itext7-dotnet)。我沒有使用iText的經驗。 –

+0

更新其他庫的答案 –

1

該庫的C#版本存在一個錯誤。請查看此拉取請求以瞭解如何在本地修復此問題,或等待iText團隊修復:github.com/itext/itext7-dotnet/pull/2 - Alexey Subach 3月9日18:20

相關問題