如何将ASP.NET网格导出为pdf

时间:2016-07-07 10:16:27

标签: javascript asp.net

我需要将asp.net网格导出为PDF格式为

  

CSS

带或不带

的样式,文字和图像
  

iTextSharp的

感谢。

1 个答案:

答案 0 :(得分:0)

将数据绑定到gridview后

  protected void btnExportPDF_Click(object sender, EventArgs e)
{
   Response.ContentType = "application/pdf";
   Response.AddHeader("content-disposition",
   "attachment;filename=GridViewExport.pdf");
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);
   GridView1.AllowPaging = false;
   GridView1.DataBind();
   GridView1.RenderControl(hw);
   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(); 
 }