zoukankan      html  css  js  c++  java
  • asp.net生成PDF文件(一)

    这个是例子是网上淘来的,哈哈,很有用的!

         首先要到网上下载itextsharp.dll,然后添加引用,主程序如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    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());
            }
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <%@ 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">
      
    <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>
  • 相关阅读:
    Kafka与Flume之集成比较
    Kafka之配置信息
    Kafka 之 Streams
    Kafka 之producer拦截器(interceptor)
    MySql(一)
    Android学习(一)
    Android学习(二)
    Android学习(三)
    Andoroid学习(四)
    Android学习(五)
  • 原文地址:https://www.cnblogs.com/zhycyq/p/3309346.html
Copyright © 2011-2022 走看看