Solved

Export gridview contents to PDF with c#

Posted on 2010-11-12
5
1,270 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regex validation 2 24
Deploying to Azure 3 27
Need help with a Crystal Report in my C#.Net application 2 14
C# LINQ ForEach() question 6 22
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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