crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Export gridview to PDF
Question Posted on 04 Sep 2011Home >> DotNet >> ASP.NET >> Export gridview to PDF

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Collections;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
   ///Code to Bind GridView1 Here
 }
 protected void btnExport_Click(object sender, EventArgs e)
 {
   HtmlForm form = new HtmlForm();
   //Below is control name to export to pdf
   form.Controls.Add(GridView1);
   StringWriter sw = new StringWriter();
   HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
   form.Controls[0].RenderControl(hTextWriter);
   string html = sw.ToString();
   Document Doc = new Document();
   //PdfWriter.GetInstance is created
   PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath
(Environment.SpecialFolder.Desktop)+ "\\getquestions.pdf", FileMode.Create));
   Doc.Open();
   Chunk c = new Chunk("Export GridView to PDF \n",FontFactory.GetFont("Verdana", 15));
   Paragraph p = new Paragraph();
   p.Alignment = Element.ALIGN_CENTER;
   p.Add(c);
   Chunk chunk1 = new Chunk("By Getproductprice.com team \n",FontFactory.GetFont("Verdana", 8));
   Paragraph p1 = new Paragraph();
   p1.Alignment = Element.ALIGN_RIGHT;
   p1.Add(chunk1);
   Doc.Add(p);
   Doc.Add(p1);
   System.Xml.XmlTextReader xmlReader =
new System.Xml.XmlTextReader(new StringReader(html));
   HtmlParser.Parse(Doc, xmlReader);
   Doc.Close();
   string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\Pdffilepath.pdf";
   ShowPdf(Path);
 }
 private void ShowPdf(string strS)
 {
   Response.ClearContent();
   Response.ClearHeaders();
   Response.ContentType = "application/pdf";
   Response.AddHeader("Content-Disposition","attachment;filename=" + strS);
   Response.TransmitFile(strS);
   Response.End();
   //Response.WriteFile(strS);
   Response.Flush();
   Response.Clear();
 }
}
0
0



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

Use of Calendar control in dotnet with code

How to improve Front End UI performance of dotnet web application?

File upload in dotnet

reverse a string

function of garbage collector






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap