iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml
本文关键字:ParseXHtml GetInstance XMLWorkerHelper tool xml iTextSharp | 更新日期: 2023-09-27 17:57:08
我正在使用 iTextSharp 版本:5.5.6
; iTextSharp XML Worker 版本:5.5.6
我从这里得到了一些代码,但是在我运行代码后,PDF文件将永远不会打开
:文件已损坏,无法修复。本地''EWHvxm9t5++
htmltext="''r'''r'''r'''r'''r' ''r' ''r' ''r' ''r' ''r' ''r''
n ''r' ''r'''r'''r'''r'''r'"
n ''r' 项目 ''r' 描述''r'
LotNo''r' Revision''r' NamePlatSN''r'
创建日期''r' 创建者''r' ''r' ''r'
''r' ''r' 100-817412-001''r' X500-G02 - ENV DWG ''r' 15020008''r' B
''r' testing123''r' 2015/4/9 12:00:00 AM''r' ULTRATCS''xma''r' ''r' ''r''
n ''r''
HTML 字符串(格式更好)如下所示:
<!DOCTYPE html>
<html lang='"en'" >
<body>
<table>
<tr>
<th> Item </th>
<th> Description</th>
<th> LotNo</th>
<th>Revision</th>
<th>NamePlatSN</th>
<th>DateCreated</th>
<th>CreatedBy</th>
</tr>
<tr>
<td> 100-817412-001</td>
<td> X500-G02 - ENV DWG </td>
<td>15020008</td>
<td> B </td>
<td>testing123</td>
<td> 4/9/2015 12:00:00 AM</td>
<td> ULTRATCS''xma</td>
</tr>
</table>
</body>
</html>
这是代码:
protected ActionResult ViewPdf(object model)
{
// Create the iTextSharp document.
Document doc = new Document();
byte[] buf;
// Set the document to write to memory.
MemoryStream memStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
writer.CloseStream = false;
doc.Open();
string htmltext = this.RenderActionResultToString(this.View(model));
using (var srHtml = new StringReader(htmltext))
{
//Parse the HTML
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
//buf = new byte[memStream.Position];
//memStream.Position = 0;
//memStream.Read(buf, 0, buf.Length);
buf = memStream.ToArray();
doc.Close();
}
// System.IO.File.WriteAllBytes(@"c:''temp'test.pdf", buf);
// Send the binary data to the browser.
return new BinaryContentResult(buf, "application/pdf");
}
}
}
怎么了?
(通过注释发现)
您需要先呼叫doc.Close()
,然后再呼叫buf = memStream.ToArray();
。这让iTextSharp知道你实际上已经完成了,它应该刷新任何缓冲区并写入PDF预告片。