iTextSharp的HTMLWorker.ParseToList()抛出的NullReferenceException抛出、HTMLWorker、iTextSharp、NullReferenceEx

2023-09-03 00:52:12 作者:山有木兮卿有意

我使用iTextSharp的第4节合并一大堆的HTML文件。它直到我需要升级到iTextSharp的的第5节是工作的罚款。

I am using iTextSharp v.4 to merge a whole bunch of html files. It was working fine until I needed to upgrade to v.5 of iTextSharp.

当我通过一个StreamReader(读取HTML文件的内容)到HTMLWorker对象的ParseToList方法的问题就来了。它抛出一个空引用异常。调试它,我可以访问的StreamReader,可以确认该文件的内容正确读取。

The problem comes when I pass a streamreader (reading the content of the html file) into the HTMLWorker object's ParseToList method. It throws a null reference exception. On debugging it I can access the streamReader and can confirm that the correct content of the file is read.

下面是code:

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块,你会看到我然后用几乎相同的code添加文本到PDF说,有一个问题。这code正常工作。当然,这让我觉得,问题出在原始的HTML字符串的内容,所以这里是字符串的内容,因为它立即被传递到解析器之前:

In the catch block you will see that I then use practically the same code to add text to the pdf to say that there was a problem. This code works fine. This of course makes me think that the problem lies in the content of the original html string, so here is the content of the string as it is immediately before being passed into the parser:

<!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>

感谢您的帮助。 hofnarwillie

Thanks for any help. hofnarwillie

推荐答案

看起来 HTMLWorker 窒息的两个&LT; HR WIDTH = 100%/&GT; 。既然你说你ugrading到V5.XX,也可能是很好的开始使用 XMLWorker 启动解析您的HTML - 开发团队推荐它。 (最新的 HTMLWorker 源$ C ​​$ C,甚至有一个小的参考指出这一点)

It looks like HTMLWorker is choking on the two <hr width="100%" />. Since you said you're ugrading to V5.XX, it might also be good to start using XMLWorker to start parsing your HTML - the development team is recommending it. (the latest HTMLWorker source code even has a small reference pointing this out)

测试与扩展HTML,它的工作原理,并不算太差实施:)

Tested with your extended HTML, it works, and isn't too bad to implement :)

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

经过测试,在网络环境中,所以替换 Response.OutputStream 您的选择。