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

最近在討論區看到有人說itextsharp可以把網頁變成PDF
小弟就去抓一下itextsharp來玩玩,先教大家最實用的,就是把GridView匯出成PDF檔

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

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

asp.net(c#)
GridViewToPDF.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewToPDF.aspx.cs" Inherits="GridViewToPDF"
    EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridViewToPDF</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="匯出至PDF檔" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

GridViewToPDF.aspx.cs 


using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class GridViewToPDF : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<member> mem = new List<member>();
            mem.Add(new member("1", "Puma"));
            mem.Add(new member("2", "F6 Team"));
            this.GridView1.DataSource = mem;
            this.GridView1.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        System.IO.StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        HtmlForm form = new HtmlForm();
        form.Controls.Add(GridView1);
        form.Controls[0].RenderControl(htmlWrite);
        StringReader reader = new StringReader(stringWrite.ToString());
        Document doc = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(doc);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        parser.Parse(reader);
        doc.Close();
    }
}

public class member
{
    private string _id;
    private string _name;

    public member(string ID, string Name)
    {
        _id = ID;
        _name = Name;
    }

    public string ID
    {
        set { _id = value; }
        get { return _id; }
    }

    public string Name
    {
        set { _name = value; }
        get { return _name; }
    }
}

執行結果:

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