Error loading the PDF Document - iTextSharp PDF
<p>I generate a PDF file using iTextSharp in ASP.NET C#. </p>

<p>I try to generate a heading + table for several element. After every element I want to start a new page. I do this with <code>doc.NewPage()</code> but when I generate my PDF file I get the following error when opening the file:</p>

<blockquote>
  <p>Error loading the PDF document</p>
</blockquote>

<p>I get this error when I try to open the PDF file in the latest version of Google Chrome. When I try to open the file in the Adobe Reader I get the following error message:</p>

<blockquote>
  <p>There was an error opening this document. The file is damaged and could not be repaired.</p>
</blockquote>

<p>The size of the PDF file is also only 1kb...</p>

<p>Here is the code how I generate the file:</p>

<pre><code>//Create a byte array that will eventually hold our final PDF
//must be outside of the foreach loop (and everything else), because we store every single generated table in here for the final pdf!!
Byte[] bytes;

List&lt;TableObject&gt; myTables = getTables();
TableObject currentTable = new TableObject();

//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (MemoryStream ms = new MemoryStream())
{
    //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
    using (Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f))
    {
        //Create a writer that's bound to our PDF abstraction and our stream
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);

        //Open the document for writing
        doc.Open();

        //writer.CloseStream = false;
        //loop all tableobjects inside the document &amp; the instance of PDFWriter itself! 
        foreach (TableObject to in myTables.ToList())
        {
            //Get the data from database corresponding to the current tableobject and fill all the stuff we need!
            DataTable dt = getDTFromID(to._tableID);
            Object[] genObjects = new Object[5];
            genObjects = gen.generateTable(dt, currentTable._tableName, currentTable._tableID.ToString(), currentTable, true);

            StringBuilder sb = (StringBuilder)genObjects[1];
            String tableName = sb.ToString();
            Table myGenTable = (Table)genObjects[0];
            //String table contains a valid html string, which works in the generate function for a single page document
            String table = genObjects[2].ToString();

            using (StringReader srHtml = new StringReader(table))
            {
                //Parse the HTML
                iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
            }

            //this will probably render a whole new page at the end of the file!! need to be fixed later!!!
            //doc.NewPage();
        }

        //After all of the PDF "stuff" above is done and closed but **before** we
        //close the MemoryStream, grab all of the active bytes from the stream
        bytes = ms.ToArray();

        doc.Close();
    } 
}

//Now we just need to do something with those bytes.
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=ShiftReport_complete.pdf");
Response.BinaryWrite(bytes);
</code></pre>

<p>How can I solve this problem so that I get rid of the error message when opening the PDF file?</p>