Export data in PDF formate form GridView
void ExportPDF1(){
GridView grdview1 = new GridView();
CssClass_GridView(grdview1);
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand("fetchDataForReportnew", conn);
cmd.Parameters.AddWithValue("@empid", Convert.ToInt64(Session["EmpId"]));
cmd.Parameters.AddWithValue("@Curr_Emp", Session["UserName"].ToString());
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Document pdfDoc = new Document(PageSize.A4, 9f, 9f, 9f, 0f);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, msReport);
if (dt.Rows.Count > 0)
{
grdview1.DataSource = dt;
grdview1.DataBind();
}
StringWriter sw0 = new StringWriter();
HtmlTextWriter hw0 = new HtmlTextWriter(sw0);
if (grdview1 != null && grdview1.Rows.Count > 0)
{
string filename = Session["UserName"].ToString() + "_Expense_" + DateTime.Now.Ticks + ".pdf";
string file_Path = "~\\ExportFiles\\" + filename;
PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath(file_Path), FileMode.Create));
pdfDoc.Open();
PdfPTable table = new PdfPTable(6);
table.TotalWidth = 578f;
table.LockedWidth = true;
PdfPCell Header1 = new PdfPCell(new Phrase("Travel Record", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL)));
Header1.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell Header2 = new PdfPCell(new Phrase("DATE OF REQUISITION", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL)));
Header1.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell name = new PdfPCell(new Phrase(dt.Rows[0]["Requisition Date"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL)));
name.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell territory = new PdfPCell(new Phrase("NAME", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL)));
territory.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell terriname = new PdfPCell(new Phrase(dt.Rows[0]["Employee"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL)));
terriname.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell submitdate = new PdfPCell(new Phrase("TERRITORY", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL)));
territory.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell date_submit = new PdfPCell(new Phrase(dt.Rows[0]["Branch"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL)));
terriname.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell Date = new PdfPCell(new Phrase("From", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
Date.BackgroundColor = new Color(80, 124, 209);
Date.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell AreaVisited = new PdfPCell(new Phrase("To", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
AreaVisited.BackgroundColor = new Color(80, 124, 209);
AreaVisited.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell CustomerName = new PdfPCell(new Phrase("Departure Date", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
CustomerName.BackgroundColor = new Color(80, 124, 209);
CustomerName.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell Product = new PdfPCell(new Phrase("Mode Of Travel", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
Product.BackgroundColor = new Color(80, 124, 209);
Product.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell Purpose = new PdfPCell(new Phrase("Purpose of visit", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
Purpose.BackgroundColor = new Color(80, 124, 209);
Purpose.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell mode = new PdfPCell(new Phrase("Fare",FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
mode.BackgroundColor = new Color(80, 124, 209);
mode.HorizontalAlignment = Element.ALIGN_CENTER;
Header1.Colspan = 6;
table.AddCell(Header1);
Header2.Colspan = 2;
table.AddCell(Header2);
name.Colspan = 4;
name.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(name);
territory.Colspan = 2;
territory.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(territory);
terriname.Colspan = 4;
terriname.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(terriname);
submitdate.Colspan = 2;
submitdate.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(submitdate);
date_submit.Colspan = 4;
date_submit.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(date_submit);
table.AddCell(Date);
table.AddCell(AreaVisited);
table.AddCell(CustomerName);
table.AddCell(Product);
table.AddCell(Purpose);
table.AddCell(mode);
pdfDoc.Add(table);
iTextSharp.text.Table datatable = new iTextSharp.text.Table(6);
datatable.Width = 100f;
datatable.Padding = 1;
datatable.Border = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
Chunk dateofvisit = new Chunk(dt.Rows[i]["From Location"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell date = new Cell(dateofvisit);
date.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(date);
Chunk area_visited = new Chunk(dt.Rows[i]["To Location"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell areavisited = new Cell(area_visited);
areavisited.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(areavisited);
Chunk customer_name = new Chunk(dt.Rows[i]["DateOfVisit"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell customername = new Cell(customer_name);
customername.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(customername);
Chunk product = new Chunk(dt.Rows[i]["ModeOfTravel"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell product1 = new Cell(product);
product1.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(product1);
Chunk purpose = new Chunk(dt.Rows[i]["Purpose Of Visit"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell purpose1 = new Cell(purpose);
purpose1.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(purpose1);
Chunk chunk = new Chunk(dt.Rows[i]["Fare"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell cell = new Cell(chunk);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(cell);
}
decimal grdtotal = 0;
for (int r = 0; r < dt.Rows.Count; r++)
{
string gd = dt.Rows[r]["Fare"].ToString();
if (gd != "")
grdtotal += Convert.ToDecimal(gd);
}
Chunk Gtotal2 = new Chunk("Grand Total (Rs.)", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD));
Cell Grand_total12 = new Cell(Gtotal2);
Grand_total12.Colspan = 5;
Grand_total12.HorizontalAlignment = Element.ALIGN_RIGHT;
datatable.AddCell(Grand_total12);
Chunk Gtotal1 = new Chunk(grdtotal.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
Cell Grand_total = new Cell(Gtotal1);
Grand_total.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell(Grand_total);
pdfDoc.Add(datatable);
Session["file_name"] = filename;
Session["file_path"] = file_Path;
Session["type"] = "Travel";
pdfDoc.Close();
}