Click here to Skip to main content
12,076,925 members (45,000 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi All,

This is Arun from chennai. I m doing a project to export the data from Gridview to PDF. Everything was fine and a new pdf document has been opened whenever I click the PDF image but there was no records displayed in it.

I have 15 records in Gridview and those records have displayed using Table Adapter. I used the Sand and Sky Auto Formatting option in Gridview and color, tablecell width, 15 rows are displayed perfectly in PDF without the text.

Please guide me what am I missing.

Thank you.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Export2PDF.DAL.DataSet1TableAdapters;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
 

namespace Export2PDF
{
    public partial class _Default : System.Web.UI.Page
    {
        DataTable1TableAdapter ds1 = new DataTable1TableAdapter();
        
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void ShowGridView(object sender, EventArgs e)
        {
            Bindgrid();
        }
 
        public void Bindgrid()
        {
            gv1.DataSource = ds1.BatchStatusByBatches(Convert.ToDateTime(TextBox1.Text), Label1.Text);
            gv1.DataBind();
        }
 
        protected void gv1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv1.PageIndex = e.NewPageIndex;
            Bindgrid();
        }
 
        public override void VerifyRenderingInServerForm(Control control)
        {
            
        }
 
        protected void PDFExportButton_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=ExportToPDF.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv1.AllowPaging = false;
            gv1.DataSource = ds1.BatchStatusByBatches(Convert.ToDateTime(TextBox1.Text), Label1.Text);
            gv1.DataBind();
            gv1.RenderControl(hw);
            gv1.HeaderRow.Style.Add("width", "15%");
            gv1.HeaderRow.Style.Add("font-size", "10px");
            gv1.Style.Add("text-decoration", "none");
            gv1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            gv1.Style.Add("font-size", "8px");
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
            Response.Clear();
        }
    }
}
Posted 31-Mar-13 21:03pm
Edited 31-Mar-13 21:07pm
SoMad155.4K
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

There would be some task where we need to Get PDF Format Document from html.

Following code is written under the button_click Event.

protected void btnExportPDF_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("contentdisposition", "attachment;filename=Investment_Declaration_Form.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.divToPdf.RenderControl(hw);
    //where divToPdf is the ContentPlaceHolder Id in which whole html contains.
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10, 10, 2, 10);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160212.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100