asp.net写入pdf文件的实现代码

发布时间:2020-12-18编辑:脚本学堂
本文介绍下,用asp.net实现写入pdf文件的一例代码,将内容导出到pdf文件中,有时会用得上,有需要的朋友,参考下吧。

1,前台代码部分
 

复制代码 代码示例:
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToPdf.aspx.cs" Inherits="ToPdf" %>
    <!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>制作导出PDF_www.jb200.com</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="导出" onclick="Button1_Click" />
        </div>
        </form>
    </body>
    </html>

2,后台代码部分
   

复制代码 代码示例:
using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using iTextSharp;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    public partial class ToPdf : System.Web.UI.Page
    {
        public 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("人月神话.pdf"), FileMode.Create));
                document.Open();
                BaseFont bfChinese = BaseFont.CreateFont("C://Windows//Fonts//SIMLI.TTF", 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));
                iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(Server.MapPath("1.jpg"));
                document.Add(jpeg);
                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));
                        table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), fontChinese));
                    }
                }
                document.Add(table);
                document.Close();
            }
            catch (DocumentException ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }
    }