2014年8月26日 星期二

ASP.NET 將網頁輸出PDF

ASP.NET PDF
將網頁物件輸出至PDF,以下使用Table為例。

1.Download the itextsharp.dll from Internet.
2.Reference of  itextsharp.dll

3.use following references of itextsharp.dll
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using iTextSharp.text.html.simpleparser;
4.Now the whole code of tsPDF.aspx.cs page will be as follows:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Configuration;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10. using System.Web;
  11. using iTextSharp.text;
  12. using iTextSharp.text.pdf;
  13. using iTextSharp.text.html.simpleparser;
  14.  
  15. public partial class Test_tsPDF : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. }
  20.  
  21. public override void VerifyRenderingInServerForm(Control control)
  22. {
  23. //required to avoid the runtime error "
  24. //Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
  25. }
  26.  
  27. private void ExportGridToPDF()
  28. {
  29. try
  30. {
  31. Response.ContentType = "application/pdf";
  32. Response.AddHeader("content-disposition", "attachment;filename=Vithal_Wadje.pdf");
  33. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  34. StringWriter sw = new StringWriter();
  35. HtmlTextWriter hw = new HtmlTextWriter(sw);
  36. //GridView1.RenderControl(hw);
  37. Table1.RenderControl(hw);
  38. StringReader sr = new StringReader(sw.ToString());
  39. Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
  40. HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
  41. PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
  42. pdfDoc.Open();
  43. htmlparser.Parse(sr);
  44. pdfDoc.Close();
  45. Response.Write(pdfDoc);
  46. Response.End();
  47. GridView1.AllowPaging = true;
  48. GridView1.DataBind();
  49. }
  50. catch (Exception)
  51. {
  52. throw;
  53. }
  54. }
  55.  
  56. protected void Button1_Click(object sender, EventArgs e)
  57. {
  58. ExportGridToPDF();
  59. }
  60. }
5.Now the whole code of tsPDF.aspx page will be as follows:
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  5. <asp:GridView ID="GridView1" runat="server"></asp:GridView>
  6. <asp:Table ID="Table1" runat="server">
  7. <asp:TableRow>
  8. <asp:TableCell>Cell 1</asp:TableCell>
  9. <asp:TableCell>Cell 2</asp:TableCell>
  10. <asp:TableCell>Cell 3</asp:TableCell>
  11. <asp:TableCell>Cell 4</asp:TableCell>
  12. <asp:TableCell>Cell 5</asp:TableCell>
  13. <asp:TableCell>Cell 6</asp:TableCell>
  14. </asp:TableRow>
  15. </asp:Table>
  16. </div>
  17. </form>
  18. </body>

Reference website:
http://www.c-sharpcorner.com/UploadFile/0c1bb2/export-gridview-to-pdf/
http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm

沒有留言:

張貼留言