C# 无法加载类型';iTextSharp.text.html.HtmlParser';来自组件';itextsharp,版本=5.5.5.0,区域性=中性,PublicKeyToken=8354ae6d2174ddca';

C# 无法加载类型';iTextSharp.text.html.HtmlParser';来自组件';itextsharp,版本=5.5.5.0,区域性=中性,PublicKeyToken=8354ae6d2174ddca';,c#,asp.net-mvc-4,itextsharp,razorpdf,C#,Asp.net Mvc 4,Itextsharp,Razorpdf,将html转换为pdf我在webconfig中遇到了这个版本错误,让一些天才发现并解决了这个问题 我的模型 public class Customer { public int CustomerID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } 我的控制器这是正常代码 public ActionResult Index()

将html转换为pdf我在webconfig中遇到了这个版本错误,让一些天才发现并解决了这个问题

我的模型

 public class Customer
  {
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
  }
我的控制器这是正常代码

 public ActionResult Index()
    {
        List<Customer> customers = new List<Customer>();

        for (int i = 1; i <= 10; i++)
        {
            Customer customer = new Customer
            {
                CustomerID = i,
                FirstName = string.Format("FirstName{0}", i.ToString()),
                LastName = string.Format("LastName{0}", i.ToString())
            };
            customers.Add(customer);
        }
        return View(customers);
    }
public ActionResult Index()
{
列出客户=新列表();

对于(inti=1;i您有几个问题

首先,您有一个版本绑定重定向:

<bindingRedirect oldVersion="0.0.0.0-5.5.5.0" newVersion="5.5.5.0" />

这是一个巨大的总括性声明,它假设在版本
0.0.0
5.5.5.0
之间没有发生API更改。然而,当API发生更改时,一些/许多/大多数/所有库都会增加其主要和次要版本号

第二,但与第一个相关,在iTextSharp 4.1.6(4.x系列中最后一个发布的iTextSharp,从Java 2.x系列移植而来)和5之间,实际上有一些API更改。在您非常特定的情况下,类
iTextSharp.text.html.HtmlParser
被删除,这就是为什么会出现异常的原因

有几种方法可以解决这个问题

选项#1-好方法

  • 摆脱RazorPDF。它已经两年半没有更新了,它需要一个过时的iTextSharp版本,并使用一个过时的HTML解析器

  • 切换到使用iTextSharp较新的HTML解析
    XmlWorker

  • 选项#2-坏方法

  • 阅读官方标题的第四个方框“为什么我不应该使用iText2.x(或iTextSharp 4.x)?”

  • 下载iTextSharp 4.1.6源代码。您需要自己查找此源代码。不要费心问从哪里获得它,因为此版本不受社区甚至软件制造商的支持

  • 请您的法律顾问逐行检查源代码,以确保其符合您所在司法管辖区的法律以及任何有关版权的国际条约。请认真

  • 如果您的法律顾问批准源代码,请编译它,删除绑定重定向并将DLL放入您的项目中

  • 接受4.4.6版本的解析器非常非常有限的事实,并有一些已知的问题会为你认为完全有效的HTML抛出异常。如果你想对这些问题提出任何支持,你会被告知两件事,升级到最新版本,并从<代码> HTMLWorks>中切换过来。/代码>到

    XmlWorker

  • 选项#3-丑陋的方式(适用于布鲁诺)

  • 下载

  • 使用4.1.6逻辑或您自己的逻辑重新实现
    iTextSharp.text.html.HtmlParser
    和所有其他缺少的类、方法和属性

  • 编译链接


  • 请阅读:RazorPDF的许可证是什么?我认为它是Apache。在这种情况下,RazorPDF使用iTextSharp是非法的。iTextSharp有一个copyleft许可证(版本5.5.5.0使用AGPL),因此它不能被RazorPDF使用。RazorPDF决不隶属于iText集团(我是该集团的首席执行官)RazorPDF绝对不被iText软件认可。我建议你尝试另一种解决方案。它怎么办?请参考我在first linerazor pdf中提供的链接Razor pdf版本为1.0.0.0你有好的,有坏的,…我错过了丑陋的:@BrunoLowagie,我为你修复了它!
     <dependentAssembly>
        <assemblyIdentity name="itextsharp" publicKeyToken="8354ae6d2174ddca" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.5.5.0" newVersion="5.5.5.0" />
      </dependentAssembly>
    
    <bindingRedirect oldVersion="0.0.0.0-5.5.5.0" newVersion="5.5.5.0" />