標籤

more tags...

最新回覆

[ASP.NET]利用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

推到 Twitter!
推到 Plurk!


 

2009/10/14 18:24| 閱讀數 : 3616 | 1 人推薦 我要推薦 | 7 Comments | 文章分類 : ASP.NET 訂閱


回覆

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by Dino on 2009/10/28 下午 12:45  回覆

可惜中文會無法顯示

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by puma on 2009/10/29 上午 08:38  回覆

to Dino,

是喔..沒測過中文..

改天再來試試有沒有其它解決方法..

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by Jerry on 2009/11/5 下午 05:29  回覆

您好:

我有嘗試著用VB的寫法以做此功能,語意編譯都正常
但在執行時會顯示錯誤在「pdfdoc.Close()」
The document has no pages.
想請教您如果改以VB語法來寫該怎麼修改呢?

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by puma on 2009/11/6 上午 08:51  回覆

to Jerry,

可能要匯出的內容是空的才會產生這個錯誤

我用一個Gridview沒有給資料,把GridView匯成PDF就會產生跟你一樣的錯誤..

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by jubo on 2010/1/11 下午 04:15  回覆

推中文= =

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by 大陆 on 2010/1/29 下午 05:08  回覆

在国外网站整理了一套资料 问题都解决了 呵呵

# re: [ASP.NET]利用itextsharp將GridView匯出PDF檔, Posted by sun on 2010/3/5 下午 04:04  回覆

可以不要GridView
直接匯出該頁顯示的資料?
可以敎我如何修改? 謝謝

發表回覆

標題: *
姓名: *
Email: (將不會被顯示)
Url:
回覆: *
登入後使用進階評論
Please add 7 and 3 and type the answer here:
F6 Team logo


用BloggerAds 替自已加薪

每月文章

文章分類

推薦討論區

推薦部落格