AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
  export gridview to pdf without using third party control
Posted by Shefali Singal on  4/29/2009 12:38:01 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
code to export gridview to pdf without using third party control.........
I am using asp.net framework 2.0 in vb

thanx in advance......

Post Your Reply Post New Question

 Viewer's Reply
Use Asp.Net Local Report
Answered By Moderator1 on 4/29/2009 10:07:41 AM
Hi,

Without using third party tool, you can do that with Local Report. That is by using Rdlc Report.
 
Grid view to pdf convertor
Answered By Kulsurya on 2/21/2010 11:44:33 PM
//just add a class file first named Mypage.cs

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;


/// <summary>
/// Summary description for Mypage
/// </summary>
public class Mypage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
GridView grid = control as GridView;
if (grid != null && grid.ID == "grdReport")
return;
else
base.VerifyRenderingInServerForm(control);

}
}


//now add iTextSharp.dll to bin folder

//now


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 System.IO;
using iTextSharp.text.html;
using System.Xml;
using iTextSharp.text.html.simpleparser;



protected void btnPDF_Click(object sender, EventArgs e)
{
try
{
grdReport.Visible = false;

Mypage tmpPage = new Mypage();
HtmlForm form = new HtmlForm();
form.Controls.Add(grdReport);
tmpPage.Controls.Add(form);
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(htmlWriter);
string htmlContent = sw.ToString();

StringReader sr = new StringReader(sw.ToString());
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document, new FileStream("c://Test.pdf", FileMode.Create));

// step 3: we open the document
document.Open();

// step 4: we add a paragraph to the document
//document.Add(new Paragraph(htmlContent.ToString()));
HTMLWorker htmlparser = new HTMLWorker(document);
System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));
//HtmlParser.Parse(document, _xmlr);
htmlparser.Parse(sr);
// step 5: we close the document
document.Close();

ShowPdf("c://Test.pdf");
}
catch (Exception ex)
{
}
}

private void ShowPdf(string s)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + s);
Response.ContentType = "application/pdf";
Response.WriteFile(s);
Response.Flush();
Response.Clear();
}
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Speech Technology Magazine
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks - Free 156 Page Preview