title: How to export a datatable to a previous created PDF file while specifying a page to do that? (or creating a new page)
body: <p>I have the following task to complete: based on a dataset, I have to fill a PDF file's form fields using <code>itextsharp</code> while also exporting the said dataset on a table "format" so it could go on the second page of said PDF file.</p>
  
  <p><a href="https://i.stack.imgur.com/8AzJa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8AzJa.png" alt="Here is the PDF file wich has the form fields"></a>
  </p>
  
  <p>The table will go below the title on the second page wich is landscape-oriented. But I'm also accepting a solution/hint which involves deleting the second page and create-write one by code-side.</p>
  
  <p>This is by far the work I've made</p>
  
  <pre><code>class PDFFormFiller
  {
      public void FillForm(UInt32 tipo, string campo, string numeroConsultas, DataSet ds)
      {
          // I use this on another specific case, dont bother with it.
          string data = System.DateTime.Today.ToLongDateString();
  
          switch (tipo)
          {
  
          case 1: // Medical report
              {
                  // Get the previous created pdf file...
                  string pdfTemplate = Application.StartupPath + "\\Templates\\Template Medico.pdf";
                  // ...and generate a new one with the filled forms and the table
                  string newFile = Application.StartupPath + "\\Relatórios\\Relatório Médico_" + data + ".pdf";
  
                  PdfReader pdfReader = new PdfReader(pdfTemplate);
                  PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
  
                  AcroFields pdfFormFields = pdfStamper.AcroFields;
  
                  // Form fields filling by the arguments up there, dont bother with'em
                  pdfFormFields.SetField("data", data);
                  pdfFormFields.SetField("medico_nome", campo);
                  pdfFormFields.SetField("numero_consultas", numeroConsultas);
  
                  // Table build
                  int numColumns = ds.Tables[0].Columns.Count;
                  PdfPTable datatable = new PdfPTable(numColumns);
                  datatable.DefaultCell.Padding = 0;
                  datatable.WidthPercentage = 100; // percentage
                  datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
                  float[] columnWidths = {20, 20, 20, 20, 40, 20, 20, 20, 20};
                  datatable.SetWidths(columnWidths);
  
                  for (int i = 0; i &lt; ds.Tables[0].Rows.Count; i++)
                  {
                      for (int j = 0; j &lt; ds.Tables[0].Columns.Count; j++)
                      {
                          datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
                          datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                          Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
                          datatable.AddCell(phrase);
                      }
                  }
  
                  // The table code goes here
  
                  // Final Message
                  string relatorio = ("Relatório para o médico" + pdfFormFields.GetField("medico_nome") + "gerado com sucesso na pasta Relatórios");
                  MessageBox.Show(relatorio);
  
                  // Form flattening set to true to avoid
                  // further edition of the form fields on the file created
                  pdfStamper.FormFlattening = true;
  
                  // Clode the pdf stamper
                  pdfStamper.Close();
                  break;
              }
  </code></pre>