selva kumar

selva kumar

  • NA
  • 557
  • 189k

exporting to pdf doesnot working prperly?

Feb 5 2014 1:57 AM
Hi friends i am using the below code to print data's in pdf format with custom table...It runs but the data does not displayed in pdf document....Can any one help me out...

Code:

protected void Btndownload_Click(object sender, EventArgs e)
{
  PdfPTable table = new PdfPTable(8);
  //actual width of table in points
  table.TotalWidth = 216f;
  //fix the absolute width of the table
  table.LockedWidth = true;
  //relative col widths in proportions - 1/3 and 2/3
  float[] widths = new float[] { 1f, 2f,3f,4f,5f,6f,7f,8f};
  table.SetWidths(widths);
  table.HorizontalAlignment = 0;
  //leave a gap before and after the table
  table.SpacingBefore = 20f;
  table.SpacingAfter = 30f;
  PdfPCell cell = new PdfPCell(new Phrase("Products"));
  cell.Colspan = 2;
  cell.Border = 0;
  cell.HorizontalAlignment = 1;
  table.AddCell(cell);
  try
  {
  Con.Open();
  SqlCommand cmd = new SqlCommand("Select T_ID,BranchName,[Type],Brand,ReceivedDate,ReceivedStock,StockOnHand,(SoldDate+' '+[Time])as SoldDate,Quantity From StockManagement Where BranchName='" + DropDownList1.SelectedItem.Text + "' and SoldDate='" + DropDownList2.SelectedItem.Text + "' ", Con);
  SqlDataReader rdr = cmd.ExecuteReader();
  {
  while (rdr.Read())
  {
  table.AddCell(rdr[0].ToString());
  table.AddCell(rdr[1].ToString());
  table.AddCell(rdr[3].ToString());
  table.AddCell(rdr[4].ToString());
  table.AddCell(rdr[5].ToString());
  table.AddCell(rdr[6].ToString());
  table.AddCell(rdr[7].ToString());
  table.AddCell(rdr[8].ToString());
  }
  }
  }
  catch (Exception ex)
  {
  Response.Write(ex.Message);
  }
   Document doc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                doc.Open();
                doc.Add(table);
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(doc);
                Response.End();
}