Export GridView To PDF Formats

Posted by Praveenjha under ASP.NET on 5/1/2013 | Points: 10 | Views : 4839 | Status : [Member] | Replies : 3
Hi All,

I have been trying to export data from GridView to pdf.

I have somewhat achieve my objective with certain issues. My exported pdf contains gridview's data along with the code written in C#.

here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using NCRUHelp.Include;
using HRICDashboard.Include;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data.SqlClient;
using System.IO;
using System.Data.OleDb;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

if (DropDownList1.SelectedValue == "PDF")
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gvReport.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gvReport);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.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();
}


In my exported pdf what i got is :

//var theForm = document.forms['aspnetForm']; if (!theForm) { theForm = document.aspnetForm; } function
__doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit(); } } //
// theForm.oldSubmit = theForm.submit; theForm.submit = WebForm_SaveScrollPositionSubmit;
theForm.oldOnSubmit = theForm.onsubmit; theForm.onsubmit = WebForm_SaveScrollPositionOnSubmit;
theForm.oldOnLoad = window.onload; window.onload = WebForm_RestoreScrollPosition; //


along with data.

please help me.


Cheere,

Praveen




Responses

Posted by: harikrishna149-10896 on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Praveen,
Try this code may helpful to u.

public void ExportToPdf(DataTable ExDataTable)
{
//Here set page size as A4

Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();

//Set Font Properties for PDF File
Font fnt = FontFactory.GetFont("Times New Roman", 12);
DataTable dt = ExDataTable;

if (dt != null)
{

PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;

//Here we create PDF file tables

for (int rows = 0; rows < dt.Rows.Count; rows++)
{
if (rows == 0)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].ColumnName.ToString(), fnt)));
PdfTable.AddCell(PdfPCell);
}
}
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), fnt)));
PdfTable.AddCell(PdfPCell);
}
}

// Finally Add pdf table to the document
pdfDoc.Add(PdfTable);
}

pdfDoc.Close();

Response.ContentType = "application/pdf";

//Set default file Name as current datetime
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");

System.Web.HttpContext.Current.Response.Write(pdfDoc);

Response.Flush();
Response.End();

}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

Thanks & Regards
Hari Krishna

Praveenjha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Praveenjha on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Harikrishna,

Thanx for your response.

I will try your code n will let u knw.

thanks & Regards,

Praveen

Praveenjha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.Biswas on: 5/2/2013 [Member] Platinum | Points: 25

Up
0
Down
Try this one

http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx

Best Regards,
Niladri Biswas

Praveenjha, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response