gpt4 book ai didi

c# - iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml 不工作

转载 作者:太空狗 更新时间:2023-10-30 01:17:39 25 4
gpt4 key购买 nike

我使用的是 iTextSharp 版本:5.5.6; iTextSharp XML Worker 版本:5.5.6

我从 Here 得到了一些代码, 但在我运行代码后,PDF 文件将永远无法打开

:The file is damaged and could not be repaired. Local\EWHvxm9t5++

htmltext="\r\n\r\n\r\n\r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n Item \r\n Description\r\n
LotNo\r\n Revision\r\n NamePlatSN\r\n
DateCreated\r\n CreatedBy\r\n \r\n \r\n
\r\n \r\n 100-817412-001\r\n X500-G02 - ENV DWG \r\n 15020008\r\n B
\r\n testing123\r\n 4/9/2015 12:00:00 AM\r\n ULTRATCS\xma\r\n \r\n \r\n
\r\n \r\n \r\n\r\n\r\n\r\n"

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

怎么了?

最佳答案

(通过评论发现)

您需要在调用 buf = memStream.ToArray(); 之前调用 doc.Close()。这让 iTextSharp 知道您实际上已经完成,它应该刷新所有缓冲区并编写 PDF 预告片。

关于c# - iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com