Solved

Export gridview contents to PDF with c#

Posted on 2010-11-12
5
1,255 Views
Last Modified: 2012-05-10
Hi Experts,

just trying to achieve the above with these codes

but i get a message saying "Adobe Reader could not open the file because it is either not a supported file type or the file has been damaged"

Do I need an extract component installed on my machine?

thanks

public static void ExportPDF(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/pdf;";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();
                //  include the gridline settings

                table.GridLines = gv.GridLines;
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
    #endregion


========================================================= 

protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.rdoStudentListExportOptions.SelectedIndex == 1)
        {
            //  the user wants all rows exported, turn off paging
            //  and rebing the grid before sending it to the export  utility
            this.gvAcademy.AllowPaging = false;
            this.gvAcademy.DataBind();
        }
        else if (this.rdoStudentListExportOptions.SelectedIndex == 2)
        {
            //  the user wants just the first 2000,
            //  adjust the PageSize and rebind
            this.gvAcademy.PageSize = 2000;
            this.gvAcademy.DataBind();
        }
        //  pass the grid that for exporting ...
        GridViewExportUtil.ExportPDF("HE_STUDENTS.pdf", this.gvAcademy);
    }

Open in new window

0
Comment
Question by:SirReadAlot
5 Comments
 
LVL 7

Expert Comment

by:niralshah
ID: 34119164
0
 
LVL 1

Accepted Solution

by:
imdadhusen earned 500 total points
ID: 34119197
Hi

Use following code to Export Gridview to PDF
Order.aspx contain Gridview
attached itextsharp.dll is a free dll
following code in HTMLtoPDF.aspx.cs to generate PDF
// Create instance of a new document
            Document doc = new Document(PageSize.A4, 10, 10, 50, 50);
            // Change   the content type to application/pdf !Important
            HttpContext.Current.Response.ContentType = "application/pdf";
            // Get   Instance of pdfWriter to be able to create 
            // the document in the OutputStream
            PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
            // Create   Style Sheet
            StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            //--styles.LoadTagStyle("ol", "leading", "16,0");
            doc.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css"));
            // Open the   document to be able to write to it
            doc.Open();
            //--styles.LoadTagStyle("li", "face", "garamond");
            //--styles.LoadTagStyle("span", "size", "8px");
            //--styles.LoadTagStyle("body", "font-family", "times new roman");
            //--styles.LoadTagStyle("body", "font-size", "10px");
            // Parse   html to PDF understandable objects
            var objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(Server.MapPath("Order.aspx"), Encoding.Default), styles);
            for (int k = 0; k < objects.Count; k++)
            {
                doc.Add((IElement)objects[k]);
            }
            // Close   the document, rendering it to the browser
            doc.Close();

Open in new window

itextsharp.dll
0
 

Author Comment

by:SirReadAlot
ID: 34119206
okay I will try all solns
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34317310
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now