首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iTextSharp HTMLWorker.ParseToList()抛出NullReferenceException

iTextSharp HTMLWorker.ParseToList()抛出NullReferenceException
EN

Stack Overflow用户
提问于 2011-12-20 18:41:47
回答 1查看 16.2K关注 0票数 9

我正在使用iTextSharp v.4来合并一大堆html文件。在我需要升级到iTextSharp的v.5之前,它一直工作得很好。

当我将一个streamreader (读取html文件的内容)传递给HTMLWorker对象的ParseToList方法时,问题就出现了。它抛出一个空引用异常。在对其进行调试时,我可以访问streamReader,并可以确认读取了正确的文件内容。

代码如下:

代码语言:javascript
运行
复制
List<IElement> objects;
try
{
    objects = HTMLWorker.ParseToList(new StringReader(htmlString), null);
}
catch (Exception e)
{
    htmlString = "<html><head></head><body><br/><br/><h2 style='color:#FF0000'>ERROR READING FILE!</h2><h3>File Excluded From Stitched Document!</h3><br/><br/><p>There was an error while trying to read the following file:</p><p><span style='color:#FF0000'>" + fileName + "</span></p></body></html>";
    objects = HTMLWorker.ParseToList(new StringReader(htmlString), null);
}

在catch块中,您将看到我使用了几乎相同的代码将文本添加到pdf中,以表明存在问题。这段代码运行良好。这当然让我认为问题出在原始html字符串的内容上,所以下面是字符串在被传递到解析器之前的内容:

代码语言:javascript
运行
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="cache-control" content="no-cache" />
</head>
<body style="font-family: Arial, Helvetica, sans-serif; font-size: 1em; margin: 0;
    padding: 0;">
    <div style="font-size: 1em; line-height: 1.25em; width: 190mm;">
        <h1 style="font-size: 1.5em; font-weight: bold; margin: 0 0 1.5em 0; text-align: center;">
            Advice Item 1</h1>
        <table border="0" style="width: 190mm; border-collapse: collapse; margin: 0 0 1.5em 0;
            width: 100%;">
            <tbody>
                <tr>
                    <td style="width: 35mm; height: 1px; line-height: 1px; font-size: 1px;">
                        &nbsp;
                    </td>
                    <td>
                    </td>
                    <td style="width: 30mm; height: 1px; line-height: 1px; font-size: 1px;">
                        &nbsp;
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td colspan="4" style="font-weight: bold;">
                        <span id="litPatchedToCC" style="text-align: right; font-weight: bold;"></span>
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        By:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        ABC
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        From:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        CC
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Date:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        29/03/2011 13:35
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        To:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        Member Practice
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Folder:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        A15-123456
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Individual:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        Miss A B Test
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <hr width="100%" />
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Of:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        Lorem &amp; Ipsum
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Species:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        Bovine
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Position:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        Member
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Item Type:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Tel:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                        0123 01234
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                    </td>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Other Nos:
                    </th>
                    <td style="font-weight: bold; padding: 2px 5px;">
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Reason For Call:
                    </th>
                    <td colspan="3" style="font-weight: bold; padding: 2px 5px;">
                        Some Reason
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                        Subject:
                    </th>
                    <td colspan="3" style="font-weight: bold; padding: 2px 5px;">
                        Some problem.
                    </td>
                </tr>
                <tr>
                    <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                    </th>
                    <td>
                    </td>
                    <th scope="row" colspan="2" style="text-align: right; font-weight: normal; padding: 2px 5px;">
                    </th>
                    <td colspan="2">
                    </td>
                </tr>
                <tr>
                    <td style="font-size: 1.5em; font-weight: bold; text-align: center;" colspan="4">
                        Internal
                    </td>
                </tr>
                <tr>
                    <td colspan="4" style="text-align: center; padding: 2px 5px;">
                        <hr width="100%" />
                    </td>
                </tr>
            </tbody>
        </table>
        <div style="padding: 2px 5px;">
            <p>
                Here we start the discussion.</p>
            <br />
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
            <br />
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>
    </div>
</body>
</html>

谢谢你的帮助。霍夫纳威利

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-20 21:56:30

看起来HTMLWorker被这两个<hr width="100%" />卡住了。既然您说您要升级到V5.XX,那么开始使用XMLWorker来解析您的HTML可能也是一件好事-开发团队正在推荐它。(最新的HTMLWorker源代码甚至有一个小参考指出了这一点)

使用扩展的HTML进行了测试,它可以工作,而且实现起来也不是太差:)

代码语言:javascript
运行
复制
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  try {
    StringReader sr = new StringReader(htmlString);
    XMLWorkerHelper.GetInstance().ParseXHtml(
      writer, document, sr
    );          
  }
  catch (Exception e) {
    throw;
  }
}

在web环境中进行了测试,因此请使用您选择的Stream替换Response.OutputStream

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8574174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档