In this demo tutorial I will show you how to export data from gridview to pdf file.
Steps are as follows:
1. Create new web page.
2. Drag & drop gridview from toolbox & bind your database (don’t use enable sorting).
<asp:GridView ID="GridView2" runat="server">
asp:GridView>
3. Drag & drop button.
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
4. You need to download one dll file named iTextSharp.dll.
5. Now add reference of this downloaded dll.
6. Now go to code behind & some of the following codes.
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
7. Now add following code in code behind.
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content0disposition",
"attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
GridView1.HeaderRow.Style.Add("width", "15%");
GridView1.HeaderRow.Style.Add("font-size", "10px");
GridView1.Style.Add("text-decoration", "none");
GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif");
GridView1.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
StreamReader reader = new StreamReader(new MemoryStream(Encoding.ASCII.Ge tBytes(sw.ToString())));
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(reader);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Now on button click , whole data from your gridview will be transferred in pdf file .
1 comments:
C# for exporting GridView to PDF document,
ExportToPdf exporter = new ExportToPdf(this.ketticGridView1);
Post a Comment