iTextSharp:“文档未打开";错误-实际上是 [英] iTextSharp: "The document is not open" error - when it actually is
问题描述
我有此代码:
private static byte[] ConvertPdfDocument(Document document, PdfPTable headerTable, PdfPTable affidavitsTable)
{
byte[] b;
using (MemoryStream ms = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
if (document.IsOpen() == false)
{
document.Open();
}
document.Add(headerTable);
document.Add(affidavitsTable);
document.Close();
writer.Close();
b = ms.ToArray();
}
return b;
}
打开文档"对象(在此方法之外使用document.Open()
,然后传入.
The "document" object is opened (using document.Open()
outside of this method then passed in.
条件document.IsOpen()
的值为True.通过查看调试器中"document"对象的私有属性,我进一步确认了该文档实际上是打开的.它表明打开"为真".
The condition document.IsOpen()
evaluates to True. I've further confirmed the document is actually open by looking at the private properties of the "document" object in the debugger; it shows that "Open" is "true".
因此,执行继续到document.Add(headerTable)
行.
Accordingly, execution moves on to the document.Add(headerTable)
line.
此时,将引发异常:文档未打开." 在调试器停止运行(由于引发该异常)的同时,使用上述两种相同的方式,我仍然看到文档已打开.
And at that point an exception is thrown: "The document is not open." And while the debugger is stopped (due to that exception being thrown), using the same two ways described above, I can still see the document is open.
那怎么可能?
我已经谷歌搜索了一段时间,但找不到任何东西,除了发布的同一问题
I've been Googling for a while but can't find anything, other than the same question posted here with no answer...
任何帮助将不胜感激.
非常感谢, 埃里泽(Eliezer)
Thanks much, Eliezer
推荐答案
在PdfWriter.GetInstance()
中使用该文档后必须将其打开,否则没有关联的编写器,并且它什么也不做.
The document must be opened after being used in PdfWriter.GetInstance()
otherwise there no writer associated and it does nothing.
这篇关于iTextSharp:“文档未打开";错误-实际上是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!