asp.net產生PDF檔案參考

來源:互聯網
上載者:User

     這個是例子是網上淘來的,哈哈,很有用的!

     首先要到網上下載itextsharp.dll,然後添加引用,主程式如下:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf;public partial class _Default : System.Web.UI.Page {    static DataTable datatable = new DataTable("testpdf");    protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            DataRow dr;            //建立Column例,可以指明例的類型,這裡用的是預設的string            datatable.Columns.Add(new DataColumn("編號"));            datatable.Columns.Add(new DataColumn("使用者名稱"));            for (int i = 1; i < 5; i++)            {                dr = datatable.NewRow();                dr[0] = System.Convert.ToString(i);                dr[1] = "測試" + System.Convert.ToString(i);                datatable.Rows.Add(dr);            }        }       }    protected void Button1_Click(object sender, EventArgs e)    {        try        {            Document document = new Document();            PdfWriter.GetInstance(document, new FileStream(Server.MapPath("Test.pdf"), FileMode.Create));            document.Open();            BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 0, 0));             //按設定的字型輸出文本            document.Add(new Paragraph(this.TextBox1.Text.ToString(), fontChinese));            //輸出圖片到PDF檔案            iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(Server.MapPath("Images/gyl.jpg"));            document.Add(jpeg01);            iTextSharp.text.Image jpeg02 = iTextSharp.text.Image.GetInstance(Server.MapPath("Images/yy.jpg"));            document.Add(jpeg02);            PdfPTable table = new PdfPTable(datatable.Columns.Count);            for (int i = 0; i < datatable.Rows.Count; i++)            {                for (int j = 0; j < datatable.Columns.Count; j++)                {                    table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), fontChinese));                }            }            document.Add(table);            document.Close();            Response.Write("<script>alert('匯出成功!');</script>");        }        catch (DocumentException de)        {            Response.Write(de.ToString());        }    }}
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>無標題頁</title></head><body>    <form id="form1" runat="server">    <div>        <br />        請輸入一些文字用於測試:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="匯出" /></div>    </form></body></html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.