System.InvalidOperationException:从iTextSharp 5.3.3更新到5.4.2后已关闭

System.InvalidOperationException:从iTextSharp 5.3.3更新到5.4.2后已关闭,exception,pdf-generation,itextsharp,document,corruption,Exception,Pdf Generation,Itextsharp,Document,Corruption,我收到一个System.invalidoOperationException:从v更新后已关闭异常。5.3.3至5.4.2 我打电话时会发生这种情况: Document doc = new Document(PageSize.A4); . . . doc.Close(); // Document is already closed hence the exception 需要注意的是,这段代码在iTextSharp 5.3.3上运行得非常完美 我对该行和PDF进行了注释,但随后iTextSh

我收到一个
System.invalidoOperationException:从v更新后已关闭
异常。5.3.3至5.4.2

我打电话时会发生这种情况:

Document doc = new Document(PageSize.A4);
.
.
.

doc.Close(); // Document is already closed hence the exception
需要注意的是,这段代码在
iTextSharp 5.3.3
上运行得非常完美


我对该行和
PDF
进行了注释,但随后
iTextSharp
开始输出损坏的
PDF
文件,Adobe Reader或Windows 8内置PDF Reader无法打开这些文件。

在Visual Studio中使用代码并利用IntelliSense,我研究了各种可能的方法在
文档
对象上。我看到还有一个名为
CloseDocument()
的附加方法,所以我更改了这一行:

doc.Close();

你猜怎么着?这东西又开始工作了。没有例外。太棒了

希望它能帮助将来可能遇到同样问题的人


好吧,好吧。。。在尝试不同的输入选项后,我又开始出现异常

我明确地说:

pdfReader.Close();

附录文档
方法中。这是在调用
doc.Close()之前发生的。在Visual Studio中使用代码并利用IntelliSense,我查看了
文档
对象上的各种可能方法。我看到还有一个名为
CloseDocument()
的附加方法,所以我更改了这一行:

doc.Close();

你猜怎么着?这东西又开始工作了。没有例外。太棒了

希望它能帮助将来可能遇到同样问题的人


好吧,好吧。。。在尝试不同的输入选项后,我又开始出现异常

我明确地说:

pdfReader.Close();
附录文档
方法中。这是在调用
doc.Close()之前发生的。刚刚注释了上面的行,异常就消失了。

使用此代码

private void ceratepdf()
    {
        using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
        {
            //step 1
            Document doc = new Document(PageSize.A4, 2f, 2f, 10f, 15f);

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
            PdfPCell cell;
            PdfPTable table = new PdfPTable(4);
            cell = new PdfPCell(new Phrase("Incident Details"));

            cell.Colspan = 4;

            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            cell.VerticalAlignment = 1;

            table.AddCell(cell);
            doc.Open();
            doc.Add(table);
            doc.Close();
        }
    }
使用此代码

private void ceratepdf()
    {
        using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
        {
            //step 1
            Document doc = new Document(PageSize.A4, 2f, 2f, 10f, 15f);

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
            PdfPCell cell;
            PdfPTable table = new PdfPTable(4);
            cell = new PdfPCell(new Phrase("Incident Details"));

            cell.Colspan = 4;

            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            cell.VerticalAlignment = 1;

            table.AddCell(cell);
            doc.Open();
            doc.Add(table);
            doc.Close();
        }
    }