Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

c# - Could not load type 'iTextSharp.text.html.HtmlParser' from assembly 'itextsharp, Version=5.5.5.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'

see this link converting html to pdf I got this version error in webconfig let some genius find and solve the qustion.

My Model

 public class Customer
  {
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
  }

My Controller this is normal code

 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);
    }

this is for pdf convert controller

public ActionResult PDF()
    {
        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 new RazorPDF.PdfResult(customers, "PDF");
    }

My webconfig

 <dependentAssembly>
    <assemblyIdentity name="itextsharp" publicKeyToken="8354ae6d2174ddca" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.5.5.0" newVersion="5.5.5.0" />
  </dependentAssembly>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've got a couple of problems.

First, you have a version binding redirect in place:

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

This is a giant blanket statement that assumes no API changes have taken place between version 0.0.0.0 and 5.5.5.0. However, some/many/most/all libraries out there increment their major and minor version numbers when there is an API change.

Second, but related to the first, between iTextSharp 4.1.6 (the last released iTextSharp in the 4.x series, ported from the Java 2.x series) and 5 there was in fact some API changes. In your very specific case, the class iTextSharp.text.html.HtmlParser was removed which is why are getting that exception.

There's a couple of ways to fix this.

Option #1 - The Good Way

  1. Get rid of RazorPDF. It hasn't been updated in two and a half years, it requires an obsolete version of iTextSharp and uses an obsolete HTML parser.

  2. Switch to using iTextSharp's newer HTML parsing XmlWorker. See this (long winded) answer for how to use it.

Option #2 - The Bad Way

  1. Read the fourth box on the official iText website's sales FAQ page title "Why shouldn't I use iText 2.x (or iTextSharp 4.x)?"

  2. Download the iTextSharp 4.1.6 source code. You'll need to look for this on your own. Don't bother asking where to get it as this version is not supported by the community or even the makers of the software.

  3. Have your legal counsel inspect the source code, line by line, to ensure that it complies with your jurisdiction's laws as well as any international treaties regarding copyrights. Seriously.

  4. If your legal counsel approves the source code, compile it, remove the binding redirect and drop the DLL into your project.

  5. Accept the fact that version 4.1.6's parser is very, very limited and has a couple of known issues that will throw exceptions for what you would consider perfectly valid HTML. Also accept that if you ask for any support for these problems you will be told two things, to upgrade to the most recent version and to switch from HTMLWorker to XmlWorker.

Option #3 - The Ugly Way (for Bruno)

  1. Download the official iTextSharp source.

  2. Re-implement the iTextSharp.text.html.HtmlParser and all other missing classes, methods and properties using either the 4.1.6 logic or your own.

  3. Compile and link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...