title: export data to pd using itextshap.dll and lot of extra space occurs
body: <p>I am using iTextSharp to export data to a PDF file and making nested PDFPTABLE and when nested datatable having very large data then there is lot of space occurs. Please see the attached image.</p>
  
  <p><a href="https://i.stack.imgur.com/8goxB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8goxB.jpg" alt="enter image description here"></a></p>
  
  <p>How can i resolve this?</p>
  
  <pre><code>           Document doc = new Document(PageSize.A4, 10, 10, 40, 35);
          MemoryStream ms = new MemoryStream();
          PdfWriter writer = PdfWriter.GetInstance(doc, ms);
          writer.InitialLeading = 0;
          //writer.CloseStream = false;
          DataTable dt1 = ds.Tables[0];
          DataTable dt2 = ds.Tables[1];
          DataTable dt3 = ds.Tables[2];
  
          DataTable newDt2 = new DataTable();
          DataTable newDt3 = new DataTable();
  
          UserSession OUser = (UserSession)HttpContext.Current.Session["User"];
          string wtUnit = OUser.UserWeightAbbriviation;
          string volUnit = OUser.UserVolumeAbbriviation;
  
  
        string userRevenueUnit = (OUser.UserCurrencyID == null ?  
  
        OUser.BUCurrencyAbbriviation : OUser.UserCurrencyAbbriviation);
  
          var normalFont = FontFactory.GetFont(FontFactory.TIMES, 6);
          var boldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 6);
  
          doc.Open();
  
          PdfPTable table = new PdfPTable(dt1.Columns.Count - 1) {       WidthPercentage = 100 };
          for (int j = 0; j &lt; dt1.Columns.Count; j++)
          {
              if (dt1.Columns[j].ColumnName == "Req_Id")
                  continue;
              if (dt1.Columns[j].ColumnName == "Wt")
                  table.AddCell(new Phrase((dt1.Columns[j].ColumnName).Replace("_", " ") + " (" + wtUnit + ")", boldFont));
              else if (dt1.Columns[j].ColumnName == "Vol")
                  table.AddCell(new Phrase((dt1.Columns[j].ColumnName).Replace("_", " ") + " (" + volUnit + ")", boldFont));
              else if (dt1.Columns[j].ColumnName == "Rate" || dt1.Columns[j].ColumnName == "Bid_Price")
                  table.AddCell(new Phrase((dt1.Columns[j].ColumnName).Replace("_", " ") + " (" + userRevenueUnit + ") /(" + OUser.UserWeightAbbriviation + ")", boldFont));
              else if (dt1.Columns[j].ColumnName == "ULD_No")
                  table.AddCell(new Phrase(dt1.Columns[j].ColumnName.Replace("_No", " #"), boldFont));
              else if (dt1.Columns[j].ColumnName == "Release_Per")
                  table.AddCell(new Phrase(dt1.Columns[j].ColumnName.Replace("_Per", " (%)"), boldFont));
              else
                  table.AddCell(new Phrase(dt1.Columns[j].ColumnName.Replace("_", " "), boldFont));
          }
          if (dt1.Rows.Count &gt;= 1 &amp;&amp; !String.IsNullOrEmpty(Convert.ToString(dt1.Rows[0][0])))
          {
  
              for (int i = 0; i &lt; dt1.Rows.Count; i++)
              {
  
                  PdfPCell[] cell = new PdfPCell[dt1.Columns.Count - 1];
                  int colRequest = 0;
                  for (int j = 0; j &lt; dt1.Columns.Count; j++)
                  {
                      if (j == 0)
                          continue;
                      cell[colRequest] = new PdfPCell(new Phrase(dt1.Rows[i][j].ToString(), normalFont));
                      colRequest = colRequest + 1;
                  }
  
                  PdfPRow row = new PdfPRow(cell);
                  table.Rows.Add(row);
  
                  PdfPTable table1 = new PdfPTable(dt2.Columns.Count - 2) { WidthPercentage = 100 };
                  //var colWidthPercentages = new[] { 6f, 10f, 10f, 10f, 5f, 20f, 8f, 8f, 8f };
                  //table1.SetWidths(colWidthPercentages);
                  var rows = (from bb in dt2.AsEnumerable()
                              where (bb.Field&lt;string&gt;("Request_ID") == Convert.ToString(dt1.Rows[i]["Req_Id"]))
                              select bb);
  
  
                  for (int k = 0; k &lt; dt2.Columns.Count; k++)
                  {
                      if (dt2.Columns[k].ColumnName == "Request_ID" || dt2.Columns[k].ColumnName == "Path_ID")
                          continue;
                      if (dt2.Columns[k].ColumnName == "Rec_Wt_Model" || dt2.Columns[k].ColumnName == "Rec_Wt_User")
                          table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + wtUnit + ")", boldFont));
                      else if (dt2.Columns[k].ColumnName == "Rec_Vol_Model" || dt2.Columns[k].ColumnName == "Rec_Vol_User")
                          table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + volUnit + ")", boldFont));
                      else if (dt2.Columns[k].ColumnName == "Route_Cost")
                          table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " ") + " (" + userRevenueUnit + ") /(" + OUser.UserWeightAbbriviation + ")", boldFont));
                      else
                          table1.AddCell(new Phrase(dt2.Columns[k].ColumnName.Replace("_", " "), boldFont));
                  }
                  if (rows.Count() &gt; 0)
                  {
                      newDt2 = rows.CopyToDataTable();
  
                      for (int i1 = 0; i1 &lt; newDt2.Rows.Count; i1++)
                      {
                          PdfPCell[] Innercell = new PdfPCell[newDt2.Columns.Count - 2];
                          int colRoute = 0;
                          for (int j1 = 0; j1 &lt; newDt2.Columns.Count; j1++)
                          {
                              if (j1 == 0 || j1 == 1)
                                  continue;
                              Innercell[colRoute] = new PdfPCell(new Phrase(newDt2.Rows[i1][j1].ToString(), normalFont));
                              colRoute = colRoute + 1;
                          }
                          PdfPRow Innerrow = new PdfPRow(Innercell);
  
                          table1.Rows.Add(Innerrow);
  
                          PdfPTable table2 = new PdfPTable(dt3.Columns.Count - 2) { WidthPercentage = 100 };
                          var rows1 = (from bb in dt3.AsEnumerable()
                                       where (bb.Field&lt;string&gt;("Request_ID") == Convert.ToString(newDt2.Rows[i1]["Request_ID"])
                                       &amp;&amp; bb.Field&lt;string&gt;("Path_ID") == Convert.ToString(newDt2.Rows[i1]["Path_ID"]))
                                       select bb);
  
                          for (int k = 0; k &lt; dt3.Columns.Count; k++)
                          {
                              if (dt3.Columns[k].ColumnName == "Request_ID" || dt3.Columns[k].ColumnName == "Path_ID")
                                  continue;
                              if (dt3.Columns[k].ColumnName == "Flt_No")
                                  table2.AddCell(new Phrase(dt3.Columns[k].ColumnName.Replace("_No", " #"), boldFont));
                              else
                                  table2.AddCell(new Phrase(dt3.Columns[k].ColumnName.Replace("_", " "), boldFont));
                          }
                          if (rows1.Count() &gt; 0)
                          {
                              newDt3 = rows1.CopyToDataTable();
                              for (int i2 = 0; i2 &lt; newDt3.Rows.Count; i2++)
                              {
                                  PdfPCell[] Innercell1 = new PdfPCell[newDt3.Columns.Count - 2];
                                  int colLeg = 0;
                                  for (int j2 = 0; j2 &lt; newDt3.Columns.Count; j2++)
                                  {
                                      if (j2 == 0 || j2 == 1)
                                          continue;
                                      Innercell1[colLeg] = new PdfPCell(new Phrase(newDt3.Rows[i2][j2].ToString(), normalFont));
                                      colLeg = colLeg + 1;
                                  }
                                  PdfPRow Innerrow1 = new PdfPRow(Innercell1);
                                  table2.Rows.Add(Innerrow1);
                              }
  
                          }
                          else
                          {
                              string rowData = "No child records to display.";
                              PdfPCell[] NoRowcell = new PdfPCell[dt3.Columns.Count - 2];// (new Phrase(rowData));
                              //   NoRowcell[0] = new PdfPCell(table) { Colspan = newDt2.Columns.Count - 2 };
                              NoRowcell[0] = new PdfPCell(new Phrase(rowData, normalFont)) { Colspan = dt3.Columns.Count - 2 };
                              PdfPRow Norow = new PdfPRow(NoRowcell);
                              table2.Rows.Add(Norow);
                          }
  
  
                          PdfPCell[] Rowcell1 = new PdfPCell[dt2.Columns.Count - 2];
                          Rowcell1[0] = new PdfPCell(table2) { Colspan = dt3.Columns.Count - 2 };
  
                          //int diff1 = (dt2.Columns.Count - 2) - (dt3.Columns.Count - 2);
  
                          //if (diff1 &gt; 0)
                          //{
                          //    Rowcell1[dt3.Columns.Count - 2] = new PdfPCell() { Colspan = 6 };
                          //    //for (var l = newDt3.Columns.Count-2; l &lt; dt2.Columns.Count-2; l++)
                          //    //{
                          //    //    Rowcell1[l] = new PdfPCell();
                          //    //}
                          //}
  
                          PdfPRow row11 = new PdfPRow(Rowcell1);
                          table1.Rows.Add(row11);
                      }
  
                  }
                  else
                  {
                      string rowData = "No child records to display.";
                      PdfPCell[] Rowcell1 = new PdfPCell[dt2.Columns.Count - 2];// (new Phrase(rowData));
                      Rowcell1[0] = new PdfPCell(new Phrase(rowData, normalFont)) { Colspan = dt2.Columns.Count - 2 };
                      // Rowcell1[0] = new PdfPCell(table1) { Colspan = dt2.Columns.Count - 2 };
                      // Rowcell1[1] = new PdfPCell(new Phrase(rowData, normalFont));
                      PdfPRow row11 = new PdfPRow(Rowcell1);
                      table1.Rows.Add(row11);
                  }
  
                  PdfPCell[] Rowcell = new PdfPCell[dt1.Columns.Count - 1];
                  Rowcell[0] = new PdfPCell(table1) { Colspan = dt2.Columns.Count - 2 };
                  int diff = (dt1.Columns.Count - 1) - (dt2.Columns.Count - 2);
  
                  if (diff &gt; 0)
                  {
                      Rowcell[dt2.Columns.Count - 2] = new PdfPCell() { Colspan = diff };
                      //for (var l = newDt2.Columns.Count-2; l &lt; dt1.Columns.Count-1; l++)
                      //{
                      //    Rowcell[l] = new PdfPCell();
                      //}
                  }
  
                  PdfPRow row1 = new PdfPRow(Rowcell);
                  table.Rows.Add(row1);
              }
          }
          else
          {
              string rowData = "No records to display.";
              PdfPCell[] Rowcell1 = new PdfPCell[dt1.Columns.Count - 2];// (new Phrase(rowData));
              //   Rowcell1[0] = new PdfPCell(table) { Colspan = dt1.Columns.Count - 2 };
              Rowcell1[0] = new PdfPCell(new Phrase(rowData, normalFont)) { Colspan = dt1.Columns.Count - 2 };
              PdfPRow row11 = new PdfPRow(Rowcell1);
              table.Rows.Add(row11);
          }
  
          doc.Add(table);
          doc.Close();
  
          Response.ContentType = "application/pdf";
          Response.AddHeader("content-disposition", "attachment; filename=ManageAllotments.pdf");
          Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
          System.Web.HttpContext.Current.Response.Write(doc);
          HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
          HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
          HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of ex
  </code></pre>