1

我正在使用 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;           
}

我已经从这里提到了 createPDF 函数。

但我遇到以下错误

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

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

4

1 回答 1

1

没人听!HTMLWorker

  • 已过时。
  • 不会被维护。
  • 它已被 XML-Worker 取代。
于 2012-08-14T12:03:57.257 回答