我正在使用 iTextSharp v.4 合并一大堆 html 文件。它工作正常,直到我需要升级到 iTextSharp v.5。
当我将流读取器(读取 html 文件的内容)传递给 HTMLWorker 对象的 ParseToList 方法时,问题就来了。它抛出空引用异常。在调试它时,我可以访问 streamReader 并可以确认读取了正确的文件内容。
代码如下:
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 block 中,您会看到我随后使用几乎相同的代码向 pdf 添加文本以说明存在问题。此代码工作正常。这当然让我认为问题出在原始 html 字符串的内容上,所以这里是字符串的内容,因为它是在传递到解析器之前:
<!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;">
</td>
<td>
</td>
<td style="width: 30mm; height: 1px; line-height: 1px; font-size: 1px;">
</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 & 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>
感谢您的帮助。霍夫纳威利
最佳答案
看起来像HTMLWorker
被两个呛到<hr width="100%" />
.既然你说要升级到 V5.XX,开始使用 XMLWorker 可能也不错开始解析您的 HTML - 开发团队推荐它。 (最新的 HTMLWorker
源代码甚至有一个小引用指出了这一点)
使用您的扩展 HTML 进行测试,它可以工作,并且实现起来还不错 :)
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环境下测试,所以替换Response.OutputStream
与 Stream
您的选择。
关于c# - iTextSharp HTMLWorker.ParseToList() 抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8574174/
当段落长度对于 ColumnText 的宽度来说太长时,如何减少换行符的高度? 我已经尝试了以下方法,因为我看到了其他回答这个问题的问题: p.Leading = 0 但这并没有产生任何影响。 我还尝
是否可以使用 iTextSharp 将句子中的单个单词加粗?我正在处理来自 xml 的大段文本,并且我试图将几个单独的单词加粗,而不必将字符串分成单独的短语。 例如: document.Add(new
不间断空格如何用于在 PdfPTable 单元格中包含多行内容。 iTextSharp 正在用空格字符分解单词。 场景是我想要在表头中显示多行内容,例如在第一行可能显示“Text1 &”,在第二行显示
我正在从 iTextSharp 创建 PDF 以供打印。我有可变长度的文本,我希望始终以最大字体大小填充固定高度的表格单元格,而不会换行。如何做到这一点? 最佳答案 首先,您需要能够测量所选字体的文本
我想使用 iTextSharp 从 pdf 文件中检索文本。但是,我无法像在 itextsharp(itext) 的 JAVA 库中那样使用 PDFTextExtractor。我需要 readPDFO
我们想在发送之前在我们的 pdf 顶部添加一个带有用户电子邮件和名称的水印。我已经编写了执行此操作的代码,并且运行良好。我想检查这是否是最好的方法。我们希望在 pdf 的顶部将水印分成两行。 ,我使用
有没有办法使用 iTextSharp 更改 PDF 中第二页的页边距? 我现在有: Document document = new Document(PageSize.A4, 144f, 72f, 1
这其实是引用Question实际上已关闭 我正在使用 ItextSharp 5.2.1。 我想使用 PdfContentByte 使我的标题文本带有下划线。请为我提供解决方案。 最佳答案 privat
我正在使用来自 nuGet (5.5.8) 的最新 iTextSharp 库来解析 pdf 文件中的一些文本。我面临的问题是 GetTextFromPage 方法不仅从它应该返回的页面中返回文本,它还
如何在保持 itextsharp 旋转的同时缩放 pdf 页面? 我有以下内容,但我失去了轮换: public static void ScaleToLetter(string inPDF,
我必须在 pdf 中插入图像。也就是说,无论我在哪里看到文本“签名”,我都必须在那里插入签名图像。我可以通过说 absolute positions 来做到。但是,我正在寻找如何在 pdf 中找到“签
我希望使用 itextSharp 将 html 转换为 pdf。 我希望在我的 pdf 中有一个特定的样式。 我希望所有 pdf 文件都遵循特定的 CSS 类。但我不知道我必须添加那个编译器 khno
我在 ASP.NET 代码中使用 iTextSharp DLL。我正在将数据提取到数据集中并将数据集添加到 PDF 表中。 如果我的数据集有更多 100 行,那么 100 行将添加到 PDF 表中,并
如何使用 iIextSharp 为 PDF 文档设置默认字体和字体大小,以便在整个 PDF 中使用它。 最佳答案 遇到与俄语和罗马尼亚字母相同的问题(itextsharp 5.5.6.0,.net 3
我使用 PdfContentByte 在 pdf 中显示文本,因为我现在也使用 SetTextMatrix mathod 来放置该文本,当我的文本很大时它不会显示在 pdf 中显示我可以包装文本显示我
我现在正在使用 iTextSharp (5.4.5) 几个星期。这周,我在文档中的元素顺序方面遇到了一些奇怪的事情。 我正在处理包含主题和图像(图表)的 pdf 报告。 文档的格式是这样的: 自然保护
我尝试了几种方法来做到这一点,但仍然无法做到。看来 iTextSharp 需要 2 次通过情况,以便图像出现在文本顶部。所以我尝试使用内存流来执行此操作,但我不断收到错误。 Public Fu
我在 iText/iTextSharp(iTextSharp 5.3.3 通过 NuGet)中遇到了一个非常奇怪的 XFA 表单问题。我正在尝试填写静态 XFA 样式的表单,但我的更改没有生效。 我有
当我使用 itextsharp 提取文本时,我将获得文本的 x 和 y 坐标。如果我根据 xy 位置将文本从 pdf 转换为 html,则通过使用这 2 个坐标,文本位置 chnages 。得到我使用
有人可以提供示例或链接到使用 itextsharp 5.4.4 签署现有 pdf 的示例吗?理想情况下保持 pdf/pdf 的一致性?谢谢。 编辑:我理解这个问题看起来好像我没有使用谷歌等。但是,新版
我是一名优秀的程序员,十分优秀!