Friday, 13 July 2012

Converting Grid to PDF

Hello friends..!
     Here in this article i will explain step by step to how to generate PDF fromat file of the data  present in ASP Gridview control. Just follow the following simple steps:

1) There is a DLL file required for this .You can Simply download from this Link  .
2) Then start a simple ASP.Net application .
3) Add a web page .
4) Place a Gridview over this page.
5) Bind this Grid with the database and show required data.
6) Place a Button and change its text to "Export to pdf".
7) Double click on this button to generate its click event.
8) Then paste the code given below:
  //add these namespaces first:

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

// code on button's click event
             Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=ResumeTracking.pdf");
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gvMainPDF.HeaderRow.Style.Add("width", "5%");
            gvMainPDF.HeaderRow.Style.Add("font-size", "10px");
            gvMainPDF.Style.Add("text-decoration", "none");
            gvMainPDF.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            gvMainPDF.Style.Add("font-size", "20px");
            gvMainPDF.FooterRow.Style.Add("font-size", "10px");

            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

//Add this override function also:
  public override void VerifyRenderingInServerForm(Control control)
        /* Verifies that the control is rendered */
This function just renders your Grid Control.

Note:  i)  The word used here "gvMainPDF" is the ID of your Grid Control. 
           ii)  The word used here "ResumeTracking.pdf" will become the name of your pdf file.

      And thats All..!!
I hope will be really helpull to you.
  Thanks for reading this article:
Dont forget to give your valuable comments..!!!

No comments:

Post a Comment