无法使用iTextSharp将HTML(http://www.google.co.in/)转换为PDF

时间:2012-08-14 10:29:37

标签: c#-4.0 itextsharp

我正在使用iTextSharp将HTML(源网站是Google:http://www.google.co.in/)转换为PDF。

我的代码:

protected void Page_Load(object sender, EventArgs e)
{  
    WebClient wc = new WebClient();
    string HTMLCode = wc.DownloadString("http://www.google.co.in/");
    var result = createPDF(HTMLCode);            
}

private MemoryStream createPDF(string html)
{
    MemoryStream msOutput = new MemoryStream();
    TextReader reader = new StringReader(html);

    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 30, 30, 30, 30);

    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

    // step 3: we create a worker parse the document
    HTMLWorker worker = new HTMLWorker(document);

    // step 4: we open document and start the worker on the document
    document.Open();
    worker.StartDocument();

    // step 5: parse the html into the document
    worker.Parse(reader);

    // step 6: close the document and the worker
    worker.EndDocument();
    worker.Close();
    document.Close();

    return msOutput;           
}

我已经从here引用了createPDF函数。

但我遇到以下错误

  

无法将类型为'iTextSharp.text.html.simpleparser.CellWrapper'的对象转换为'iTextSharp.text.Paragraph'。

iTextSharp库有问题吗?顺便说一句,我使用 itextsharp-dll-core-5.3.0

1 个答案:

答案 0 :(得分:1)

没有人听! HTMLWorker

  • 已过时。
  • 将无法维护。
  • 它被XML-Worker取代。
相关问题