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();
}
}
}