?
Solved

Export gridview contents to PDF with c#

Posted on 2010-11-12
5
Medium Priority
?
1,312 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
4 Comments
 
LVL 7

Expert Comment

by:niralshah
ID: 34119164
0
 
LVL 1

Accepted Solution

by:
imdadhusen earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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