[ASP.NET]利用itextsharp将GridView汇出PDF档

原文 [ASP.NET]利用itextsharp将GridView汇出PDF档

最近在讨论区看到有人说itextsharp可以把网页变成PDF

小弟就去抓一下itextsharp来玩玩,先教大家最实用的,就是把GridView汇出成PDF档

asp.net(c#) 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="GridViewToPDF.Index" %>



<!DOCTYPE html>



<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>

 

 

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;



namespace GridViewToPDF

{

    public partial class Index : 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

你可能感兴趣的:(GridView)