To Export Pdf file
28/02/2012 16:49
//We have to download iTextSharp assambly(that id dll file).
//And put these header file in our page
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
//We have to create gridview(any control) to convert pdf
//we have to put this override function after that pageload if you are using master page
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
//else you have to create gridview dynamically and then do this action
//gvQuestionPaper.DataSource = dt;
//gvQuestionPaper.DataBind();
gvQuestionPaper.Columns.Clear();
gvQuestionPaper.AutoGenerateColumns = false;
gvQuestionPaper.DataSource = dt;
BoundField bFld1 = new BoundField();
BoundField bFld2 = new BoundField();
BoundField bFld3 = new BoundField();
bFld1.DataField = "Q_ID";
bFld2.DataField = "Question_desc";
bFld3.DataField = "Marks";
gvQuestionPaper.Columns.Add(bFld1);
gvQuestionPaper.Columns.Add(bFld2);
gvQuestionPaper.Columns.Add(bFld3);
gvQuestionPaper.DataBind();
gvQuestionPaper.HeaderRow.Visible = false;
gvQuestionPaper.Width = Unit.Percentage(100);
gvQuestionPaper.Style.Add("text-decoration", "none");
gvQuestionPaper.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvQuestionPaper.Style.Add("font-size", "7px");
gvQuestionPaper.GridLines = GridLines.None;
gvQuestionPaper.RowStyle.HorizontalAlign = HorizontalAlign.Left;
string dtstr = DateTime.Now.ToShortDateString().Replace('/', '_');
string timeStr = (DateTime.Now.ToString("HH:mm:ss tt").Replace(':', '_')).Replace(' ', '_');
string filename = testName + dtstr + timeStr;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//Gridview control for render
gvQuestionPaper.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4_LANDSCAPE, 20f, 50f, 50f, 50f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();