How to export griddata into pdf


How to export griddata into pdf

step1)Download attached file named iTextSharp.
step2)Add reference of itextSharp in your project.
step3)Include these namespaces
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
step4)Add following code


public static void ExportToPdf(GridView pgv, object pDataSource, string pFileName, string pRemoveColumns)
{

pgv.DataSource = pDataSource;
if (pgv.Rows.Count > 0)
{
pgv.AllowPaging = false;
pgv.AllowSorting = false;
pgv.BottomPagerRow.Visible = false;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + pFileName + "\"");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
ClearControls(pgv);
pgv.GridLines = GridLines.Both;
pgv.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
string[] arrRemoveColumns;
if (pRemoveColumns.Trim().Length > 0)
{
arrRemoveColumns = pRemoveColumns.Split(',');

for (int i = 0; i < arrRemoveColumns.Length; i++)
{
pgv.Columns[Convert.ToInt32(arrRemoveColumns[i])].Visible = false;
}
}

pgv.DataBind();

pgv.RenderControl(oHtmlTextWriter);
StringReader sr = new StringReader(oStringWriter.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
pgv.Controls.Clear();
pgv.AllowPaging = true;
pgv.AllowSorting = true;
pgv.DataBind();
}


}


Related Articles

Export to excel

this is a code snippet for downloading gridview contents to excel

More articles: Export to pdf Gridview export to pdf Gridview export

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: