How to Convert Gridview to PDF

Posted by Chitrasathiya under C# on 3/11/2010 | Views : 9790 | Status : [Member] | Replies : 5
How to convert gridview values to PDF in C#? I am getting error when i write the below code. Can you pls tell me how to do?adobe reader could not open 'test[1].pdf' because it is either not supported file type or because the file has been damaged LoadGrid();
GridView oGridView = new GridView();
oGridView.DataSource = dsResult.Tables[0];
oGridView.DataBind();
string attachment = "attachment; filename=Abend.pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
Response.ContentEncoding = Encoding.Unicode;
Response.BinaryWrite(Encoding.Unicode.GetPreamble());
Response.Charset = "";
this.EnableViewState = false;
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("\r\n");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
oGridView.RenderControl(hw);
Response.AppendHeader("content-disposition", "attachment;filename=" + attachment);
Response.Write(tw.ToString());
Response.End();




Responses

Posted by: Vuyiswamb on: 3/11/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
i prefare third party componets(Infragistics,telerik, ComponentOne UI ) to do this for me, You can use TextSharp i thinks its a open source component and i stand for correction. look at this example

http://forums.asp.net/p/1103953/3657150.aspx#3657150

http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

Thank you for Posting at Dotnefunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Chitrasathiya on: 3/11/2010 [Member] Starter

Up
0
Down
I couldnt download from here. Pls send me the code for TextSharp libraray.

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

Posted by: Vuyiswamb on: 3/11/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
I have Just attach it for you.

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

 Download source file

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Chitrasathiya on: 3/11/2010 [Member] Starter

Up
0
Down
DataTable dt = dddd2;
if (dt == null)
{
LoadSummarycount();
dt = dddd2;
}
dddd2 = null;
if (dt != null)
{
GridView oGridView = new GridView();
oGridView.DataSource = dsresultLoadSummaryCount.Tables[0];
oGridView.DataBind();
Response.Clear();

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

oGridView.RenderControl(htw);

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=Abend.pdf");

Document document = new Document();
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
string html = sb.ToString();
XmlTextReader reader = new XmlTextReader(new StringReader(html));
HtmlParser.Parse(document, reader);

// Response.Write(sw.ToString());


document.Close();
sw.Close();
Response.Flush();
Response.End();
I have wriiten the above code. It is coming. But i want to fill different colors in that pdf.
 Download source file

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

Posted by: Vuyiswamb on: 3/11/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Am not that Familiar with this Component, i have used it twice, but as i stated my Company uses Telerik and Infragistics. but look at this
http://itextsharp.sourceforge.net/tutorial/ch10.html

http://itextsharp.sourceforge.net/examples/Chap1013.cs

http://itextsharp.sourceforge.net/tutorial/ch01.html

http://itextsharp.sourceforge.net/examples/Chap1015.cs

Thank you for posting at Dotnetfunda
[Administrator]

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

Login to post response