ITextSharp: Document Not Open Error - When It Really Is

I have this code:

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

The document object is opened (using document.Open()outside this method, then passed.

The condition document.IsOpen()is True. I also confirmed that the document is indeed open by looking at the private properties of the "document" object in the debugger; it shows that "Open" is "true".

Accordingly, execution proceeds to the line document.Add(headerTable).

And at this moment an exception is thrown: "The document is not open." And although the debugger is stopped (due to the fact that this exception is thrown) using the same two methods as described above, I can still see that the document is open.

How could this be?

Google, , , ...

.

,

+4
2

PdfWriter.GetInstance(), .

+8

for

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("D:\\addLife271118\\src\\assets\\finalbill.pdf"));
document.open();

try {       
    document.add(new Paragraph(" "));
    String[] names= {"james","siva"};

    for(int i= 0; i< names.length;i++)
    {
      document.add(new Paragraph(names[i]));
      document.add(Chunk.NEWLINE);
    }   
} catch (DocumentException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

document.close();
0

All Articles