Click here to Skip to main content
15,943,085 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
c# code for exporting gridview data into pdf
Posted
Comments
bbirajdar 13-Mar-13 0:33am    
You just posted the title..Where is the code? You forgot to post it...

you can download iTextSharp.dll using google search. . .

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

C#
class string pdf()
{
Response.Clear();
            Response.BufferOutput = true;
            Response.ContentType = "Application/pdf";
            Response.Cache.SetCacheability(HttpCacheability.Private); // set as private for HTTPS dowload issue with IE
            Response.AppendHeader("Content-Disposition", "attachment;filename=Classes_" + cmbsubcategory.SelectedValue.ToString() + "_" + cmbstatus.SelectedItem.ToString() + "_" + DateTime.Now.ToString() + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gridbind(cmbsubcategory.SelectedValue.ToString(), cmbstatus.SelectedValue.ToString());
            GridView1.AllowPaging = false;

            GridView1.DataBind();
            GridView1.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();
            Paragraph footer = new Paragraph("Category:Class | Subcategory:" + cmbsubcategory.SelectedValue.ToString() + "| Status:" + cmbstatus.SelectedItem.ToString() + "| " + DateTime.Now.ToShortDateString(), FontFactory.GetFont(FontFactory.COURIER, 14, iTextSharp.text.Font.NORMAL, new Color(0, 163, 217)));
            footer.Alignment = Element.ALIGN_RIGHT;
            PdfPTable footerTbl = new PdfPTable(1);
            footerTbl.TotalWidth = 300;
            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

            string imageFilePath = GetUrl("header.jpg");

            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

            jpg.Alignment = Element.ALIGN_CENTER;



            PdfPCell cell = new PdfPCell(footer);
            pdfDoc.Add(jpg);
            pdfDoc.Add(footer);


            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
}
 
Share this answer
 
Comments
Member 8632652 13-Mar-13 1:12am    
i have try this code


nt rindex = (((GridViewRow)(((LinkButton)(sender)).Parent.BindingContainer))).RowIndex;
string IId = GridView1.DataKeys[rindex]["Id"].ToString();
Download1(IId);

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);
// GridView1.AllowPaging = false;
// GridView1.DataBind();
GridView1.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();


htmlparser.Parse(sr) gives "Could not find a part of the path 'C:\Download\images (3)'." error
how can i solve this
[no name] 13-Mar-13 1:14am    
on which line you got this error ?
ITextSharp[^] is a very neat third party that would allow you to do this.
 
Share this answer
 
Hello,

Have a look at this article: Export Data to Excel, Word, PDF without Automation from DataBase.

It introduces how to export data from database to datagridview and then to PDF and other document format.

Hope that you can get something from it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900