title: how to adjust Pdf with tabular form with itextSharp in asp.net 3.5
body: <p>i 'm working with export details from database to PDF using iTextSharp library. here i just want to result as below:</p>
  
  <pre><code>Parent Table
  Enquiry Details
  ...........
  
     IF(Enquiry Have Reply)
     Create First Child Table
     ..............
     ..............
        If(Reply have File)
        Create Second Child Table
         ..........
         ..........
  </code></pre>
  
  <p><strong>--------------------------------------------------------Updated-------------------------------------------------------------</strong></p>
  
  <p>Report returned with Nested Table scenario. Here is my code:</p>
  
  <pre><code>protected void lbut_print1_Click(object sender, EventArgs e)
  {
      DataRow dr = GetData("SELECT Person_name,Company_name, Address, City_name," +
                           "(SELECT [Country_name] FROM [FileSystem].[dbo].[tbl_countries] WHERE (Id = Country_id)) AS CountryName," +
                           "(SELECT [state_name] FROM [FileSystem].[dbo].[tbl_states] WHERE (Id = State_id)) AS StateName, Mobile_no, Phone_no, Email_address, Requirement_text, Reference_text, " +
                           "Remarks_txt, IsReplied, Created_datetime FROM tbl_Enquiry_History WHERE Id =" + enquiry_id.Value.ToString()).Rows[0];
      Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
      Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
      using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
      {
          PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
          Phrase phrase = null;
          PdfPCell cell = null;
          PdfPTable table = null;
          Color color = null;
  
          document.Open();
  
          //Header Table
          table = new PdfPTable(2);
          table.TotalWidth = 500f;
          table.LockedWidth = true;
          table.SetWidths(new float[] { 0.3f, 0.7f });
  
          //Company Logo
          cell = ImageCell("../images/easyweb_logo.gif", 30f, PdfPCell.ALIGN_CENTER);
          table.AddCell(cell);
  
          //Company Name and Address
          phrase = new Phrase();
          phrase.Add(new Chunk("Electrocom Technology India Limited\n\n", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
          phrase.Add(new Chunk("712, Sukhsagar Complex,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          phrase.Add(new Chunk("Nr. Hotel Fortune Landmark, Ashram Road,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          phrase.Add(new Chunk("Ahmedabad - 380 014,", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          phrase.Add(new Chunk("Gujarat (INDIA).", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
          cell.VerticalAlignment = PdfCell.ALIGN_TOP;
          table.AddCell(cell);
  
          //Separater Line
          color = new Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));
          DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color);
          DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color);
          document.Add(table);
  
          table = new PdfPTable(2);
          table.HorizontalAlignment = Element.ALIGN_LEFT;
          table.SetWidths(new float[] { 0.3f, 1f });
          table.SpacingBefore = 20f;
  
          //Enquiry Detail
          cell = PhraseCell(new Phrase("\n" + "Enquiry Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, Color.BLACK)), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          table.AddCell(cell);
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 30f;
          table.AddCell(cell);
          document.Add(table);
  
          table = new PdfPTable(2);
          table.SetWidths(new float[] { 0.5f, 2f });
          table.TotalWidth = 350f;
          table.LockedWidth = true;
          table.SpacingBefore = 20f;
          table.HorizontalAlignment = Element.ALIGN_RIGHT;
  
          //Person Name
          table.AddCell(PhraseCell(new Phrase("Person Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Person_name"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Company Name
          table.AddCell(PhraseCell(new Phrase("Company Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Company_name"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Address
          table.AddCell(PhraseCell(new Phrase("Address :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Address"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //City Name
          table.AddCell(PhraseCell(new Phrase("City Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["City_name"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Country Name
          table.AddCell(PhraseCell(new Phrase("Country Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["CountryName"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //State Name
          table.AddCell(PhraseCell(new Phrase("State Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["StateName"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Mobile No
          table.AddCell(PhraseCell(new Phrase("Mobile No :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Mobile_no"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Phone No
          table.AddCell(PhraseCell(new Phrase("Phone No :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Phone_no"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Email Address
          table.AddCell(PhraseCell(new Phrase("Email Address :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Email_address"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Requirement
          table.AddCell(PhraseCell(new Phrase("Requirement :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Requirement_text"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Reference
          table.AddCell(PhraseCell(new Phrase("Reference :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Reference_text"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Remarks
          table.AddCell(PhraseCell(new Phrase("Remarks :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(dr["Remarks_txt"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
  
          //Created Date
          table.AddCell(PhraseCell(new Phrase("Created Date :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
          phrase = new Phrase(new Chunk(string.Format("{0:F}", dr["Created_datetime"]) + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
          cell.Colspan = 2;
          cell.PaddingBottom = 10f;
          table.AddCell(cell);
          document.Add(table);
  
          table = new PdfPTable(2);
          table.SetWidths(new float[] { 0.5f, 2f });
          table.TotalWidth = 340f;
          table.LockedWidth = true;
          table.SpacingBefore = 20f;
          table.HorizontalAlignment = Element.ALIGN_RIGHT;
          if (dr["IsReplied"].ToString()== "True")
          {
              DataRow dr1 = GetData("SELECT [Id],[Title],[Description],[IsFileAttached],[Created_datetime],[TotalFileSize] FROM [FileSystem].[dbo].[tbl_Replies] WHERE Enquiry_History_id = "+ enquiry_id.Value.ToString()+" ORDER BY  Created_datetime DESC").Rows[0];
              for (int i = 0; i &lt; dr1.Table.Rows.Count; i++)
              {
                  //Reply No
                  table.AddCell(PhraseCell(new Phrase("Reply No :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                  phrase = new Phrase(new Chunk((i+1).ToString()+ "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                  table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                  cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                  cell.Colspan = 2;
                  cell.PaddingBottom = 10f;
                  table.AddCell(cell);
  
                  //Title
                  table.AddCell(PhraseCell(new Phrase("Title :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                  phrase = new Phrase(new Chunk(dr1.Table.Rows[i][1].ToString() + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                  table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                  cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                  cell.Colspan = 2;
                  cell.PaddingBottom = 10f;
                  table.AddCell(cell);
  
                  //Description
                  table.AddCell(PhraseCell(new Phrase("Description :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                  phrase = new Phrase(new Chunk((System.Text.RegularExpressions.Regex.Replace(dr1.Table.Rows[i][2].ToString(),@"&lt;(.|\n)*?&gt;",string.Empty)) + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                  table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                  cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                  cell.Colspan = 2;
                  cell.PaddingBottom = 10f;
                  table.AddCell(cell);
  
                  //Created Date
                  table.AddCell(PhraseCell(new Phrase("Created Date :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                  phrase = new Phrase(new Chunk(string.Format("{0:F}", dr1.Table.Rows[i][4]) + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                  table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                  cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                  cell.Colspan = 2;
                  cell.PaddingBottom = 10f;
                  table.AddCell(cell);
                  document.Add(table);
  
                  table = new PdfPTable(2);
                  table.SetWidths(new float[] { 0.5f, 2f });
                  table.TotalWidth = 340f;
                  table.LockedWidth = true;
                  table.SpacingBefore = 20f;
                  table.HorizontalAlignment = Element.ALIGN_RIGHT;
                  if (dr1.Table.Rows[i]["IsFileAttached"].ToString() == "True")
                  {
                      DataRow dr2 = GetData("SELECT [File_Title],[File_Size] FROM [FileSystem].[dbo].[tbl_Reply_Files] WHERE Reply_id="+dr1.Table.Rows[i][0].ToString()).Rows[0];
                      table.AddCell(PhraseCell(new Phrase("File Details :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                      phrase = new Phrase(new Chunk("\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                      cell.Colspan = 2;
                      cell.PaddingBottom = 10f;
                      table.AddCell(cell);
                      for (int j = 0; j &lt; dr2.Table.Rows.Count; j++)
                      {
                          table.AddCell(PhraseCell(new Phrase(dr2.Table.Rows[j][0].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                          phrase = new Phrase(new Chunk(dr2.Table.Rows[j][1].ToString() + " KB" + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                          table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                          cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                          cell.Colspan = 2;
                          cell.PaddingBottom = 10f;
                          table.AddCell(cell);
                      }
                      table.AddCell(PhraseCell(new Phrase("Total File Size :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
                      phrase = new Phrase(new Chunk(dr1.Table.Rows[i][5] + " KB" + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
                      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
                      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
                      cell.Colspan = 2;
                      cell.PaddingBottom = 10f;
                      table.AddCell(cell);
                      document.Add(table);
                  }
              }
          }
          document.Close();
          byte[] bytes = memoryStream.ToArray();
          memoryStream.Close();
          Response.Clear();
          Response.ContentType = "application/pdf";
          Response.AddHeader("Content-Disposition", "attachment; filename=EnquiryRecord.pdf");
          Response.ContentType = "application/pdf";
          Response.Buffer = true;
          Response.Cache.SetCacheability(HttpCacheability.NoCache);
          Response.BinaryWrite(bytes);
          Response.End();
          Response.Close();
      }
  }
  </code></pre>
  
  <p>how ever this returns improper tabular formate report. Is there i do mistake for placing documnt.Add(table).</p>
  
  <p>this resulted pdf screen shot :</p>
  
  <p><img src="https://i.stack.imgur.com/aCn1g.png" alt="enter image description here"></p>
  
  <p>here j -loop goes wrong and generates two tables...</p>