Gridview To Pdf in ASP.NET

Posted by Prabhakar under ASP.NET on 2/2/2011 | Points: 10 | Views : 10048 | Status : [Member] [MVP] | Replies : 12
Hi friends

i want to export gridview to pdf without third party tool .. . i have lots example
but no one working .. any one suggest me .

Best Regard's
Prabhakar



Responses

Posted by: Karthikanbarasan on: 2/2/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Check this link.,.. Same problem which u face has been fixed with some resolution... it will be useful for you

http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=109866

Thanks
Karthik
www.f5Debug.net

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 2/2/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Karthikanbarasan

thanks for reply ... but it's not working fine ... lot's up petch up in code... firstly go with crystel report then create pdf ... bugg's are come ... i want to proper solution ... i am using like Excel to pdf is this ok runing these code ...

string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);

//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

say other example ..



Best Regard's
Prabhakar

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/2/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi Prabhakar,

Can ur requirement use any open source proces as well??? like some 3rd party dlls which is open source?

Thanks
Karthik
www.f5Debug.net

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 2/2/2011 [Member] Starter | Points: 25

Up
0
Down
hi karthik,

Can you refer me some of them open source dll for this requirement,

I also trying to solve the same...



Sathish Kumar S

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/2/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Sure sathish

You can download the dll from
http://sourceforge.net/projects/itextsharp/and the code is in c#

using iTextSharp.text;

using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

protected void Button5_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvEmployee.AllowPaging = false; //gvEmployee is name of GridView
gvEmployee.DataBind(); gvEmployee.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}


Thanks
Karthik
www.f5Debug.net

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 2/2/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks karthick,

I ill let you know it its works fine fr me...

Sathish Kumar S

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/2/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Sure sathish!!!

Thanks
Karthik
www.f5Debug.net

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 2/3/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Karthikanbarasan ,

u put example it's a genrate one bugg ... i am solved this problem .. Gridivew to pdf with iTextSharp.dll


Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
//gv.AllowPaging = false; //gvEmployee is name of GridView

//gv.DataBind();
//gv.RenderControl(hw);
frm.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

Best Regard's
Prabhakar

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 2/3/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Yup...You may use iTextSharp.dll it's nice once for playing with PDFs...

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/3/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Thanks prabhakar,

What error it was shwoing?

Thanks
Karthik
www.f5Debug.net

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 2/3/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Karthikanbarasan ,

Bug genrate on this Line

gv.RenderControl(hw);

gridview not rendering on this line ...



Best Regard's
Prabhakar

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/3/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Ok thanks prabhakar

Thanks
Karthik
www.f5Debug.net

Prabhakar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response