Solved

Export gridview contents to PDF with c#

Posted on 2010-11-12
5
1,287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

738 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