Converting grid to pdf in asp.net
after converting the gridview to pdf the headers & lines are not appearing in pdf. Its urgent
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted May 4, 2012, 10:44 AM EDT
Try this...
Get itextsharp.dll file from http://sourceforge.net/projects/itextsharp/
First add the itextsharp.dll file into the application as click solution explorer- Right click on your application file-Add reference-On browse tab search the itextsharp.dll file from your computer-Click ok-Finish. Then you will notice that a Bin folder will be created where you will find itextsharp.dll file present inside that folder.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Export_gridview_data_to_pdf._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
using iTextSharp.text;
namespace Export_gridview_data_to_pdf
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=student.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter StringWriter1 = new StringWriter();
HtmlTextWriter HtmlTextWriter1 = new HtmlTextWriter(StringWriter1);
GridView1.RenderControl(HtmlTextWriter1);
StringReader StringReader1 = new StringReader(StringWriter1.ToString());
Document newDocument = new Document(PageSize.A4, 7f, 7f, 7f, 7f);
HTMLWorker HTMLWorker1 = new HTMLWorker(newDocument);
PdfWriter.GetInstance(newDocument, Response.OutputStream);
newDocument.Open();
HTMLWorker1.Parse(StringReader1);
newDocument.Close();
Response.Write(newDocument);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer