title: How do I insert a table into a PDF document with iTextSharp?
body: <p>I want to insert a table into my existing PDF document.</p>
  
  <p>I was following a post about inserting an image into a PDF but I'm getting an null reference exception error when trying to add the table.</p>
  
  <p>Here is my current code</p>
  
  <pre><code>public static byte[] InsertTable(byte[] pdf, DataTable dt, int pageNum, int x, int y, int columns, int rows, int[] columnWidths, float rowHeight)
  {
      using (var inputPDF = new MemoryStream(pdf))
      using (var outputPDF = new MemoryStream())
      {
          var reader = new PdfReader(inputPDF);
          var stamper = new PdfStamper(reader, outputPDF);
          var pdfContentByte = stamper.GetOverContent(pageNum);
  
  
          Table t = new Table(columns, rows);
          t.SetWidths(columnWidths);
  
          foreach (DataRow dr in dt.Rows)
              foreach (object o in dr.ItemArray)
              {
                  Cell c = new Cell();
                  c.Add(new Chunk(o.ToString()));
                  t.AddCell(c);
              }
          pdfContentByte.PdfDocument.Add(t);
          pdfContentByte.PdfDocument.Close();
          return outputPDF.ToArray();
      }
  }
  </code></pre>