[ASP.NET]利用itextsharp將GridView匯出PDF檔

[ASP.NET]利用itextsharp將GridView匯出PDF檔

最近在討論區看到有人說itextsharp可以把網頁變成PDF

小弟就去抓一下itextsharp來玩玩,先教大家最實用的,就是把GridView匯出成PDF檔

asp.net(c#)
GridViewToPDF.aspx

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewToPDF.aspx.cs" Inherits="GridViewToPDF"
02     EnableEventValidation="false" %>
03   
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head runat="server">
07     <title>GridViewToPDF</title>
08 </head>
09 <body>
10     <form id="form1" runat="server">
11     <div>
12         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="匯出至PDF檔" />
13         <asp:GridView ID="GridView1" runat="server">
14         </asp:GridView>
15     </div>
16     </form>
17 </body>
18 </html>

GridViewToPDF.aspx.cs 

01 using System;
02 using iTextSharp.text;
03 using iTextSharp.text.pdf;
04 using iTextSharp.text.html.simpleparser;
05 using System.IO;
06 using System.Web;
07 using System.Web.UI;
08 using System.Web.UI.HtmlControls;
09 using System.Collections.Generic;
10   
11 public partial class GridViewToPDF : System.Web.UI.Page
12 {
13     protected void Page_Load(object sender, EventArgs e)
14     {
15         if (!IsPostBack)
16         {
17             List<member> mem = new List<member>();
18             mem.Add(new member("1", "Puma"));
19             mem.Add(new member("2", "F6 Team"));
20             this.GridView1.DataSource = mem;
21             this.GridView1.DataBind();
22         }
23     }
24   
25     protected void Button1_Click(object sender, EventArgs e)
26     {
27         Response.ContentType = "application/pdf";
28         Response.ContentEncoding = System.Text.Encoding.UTF8;
29         Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
30         Response.Cache.SetCacheability(HttpCacheability.NoCache);
31         System.IO.StringWriter stringWrite = new StringWriter();
32         System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
33   
34         HtmlForm form = new HtmlForm();
35         form.Controls.Add(GridView1);
36         form.Controls[0].RenderControl(htmlWrite);
37         StringReader reader = new StringReader(stringWrite.ToString());
38         Document doc = new Document(PageSize.A4);
39         HTMLWorker parser = new HTMLWorker(doc);
40         PdfWriter.GetInstance(doc, Response.OutputStream);
41         doc.Open();
42         parser.Parse(reader);
43         doc.Close();
44     }
45 }
46   
47 public class member
48 {
49     private string _id;
50     private string _name;
51   
52     public member(string ID, string Name)
53     {
54         _id = ID;
55         _name = Name;
56     }
57   
58     public string ID
59     {
60         set { _id = value; }
61         get { return _id; }
62     }
63   
64     public string Name
65     {
66         set { _name = value; }
67         get { return _name; }
68     }
69 }

執行結果:

demopdf

 

參考網址:

http://csharpdotnetfreak.blogspot.com/2008/12/export-gridview-to-pdf-using-itextsharp.html
http://blog.rubypdf.com/2007/10/10/using-htmlworker-to-parse-html-snippets-and-convert-to-pdf/
http://stackoverflow.com/questions/1322303/html-to-pdf-some-characters-are-missing-itextsharp
http://coolwebdeveloper.com/2008/10/using-open-source-itextsharp-net-pdf-library-to-generate-pdf-on-the-fly/
http://forums.asp.net/t/1199774.aspx

posted @ 2010-08-27 09:19  peterlee  阅读(649)  评论(0编辑  收藏  举报