• asp.net生成PDF文件参考


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

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

    01 using System;
    02 using System.Data;
    03 using System.Configuration;
    04 using System.Web;
    05 using System.Web.Security;
    06 using System.Web.UI;
    07 using System.Web.UI.WebControls;
    08 using System.Web.UI.WebControls.WebParts;
    09 using System.Web.UI.HtmlControls;
    10 using System.IO;
    11 using iTextSharp;
    12 using iTextSharp.text;
    13 using iTextSharp.text.pdf;
    14   
    15 public partial class _Default : System.Web.UI.Page 
    16 {
    17     static DataTable datatable = new DataTable("testpdf");
    18     protected void Page_Load(object sender, EventArgs e)
    19     {
    20         if (!Page.IsPostBack)
    21         {
    22             DataRow dr;
    23             //建立Column例,可以指明例的类型,这里用的是默认的string
    24             datatable.Columns.Add(new DataColumn("编号"));
    25             datatable.Columns.Add(new DataColumn("用户名"));
    26             for (int i = 1; i < 5; i++)
    27             {
    28                 dr = datatable.NewRow();
    29                 dr[0] = System.Convert.ToString(i);
    30                 dr[1] = "测试" + System.Convert.ToString(i);
    31                 datatable.Rows.Add(dr);
    32             }
    33         }   
    34     }
    35     protected void Button1_Click(object sender, EventArgs e)
    36     {
    37         try
    38         {
    39             Document document = new Document();
    40             PdfWriter.GetInstance(document, new FileStream(Server.MapPath("Test.pdf"), FileMode.Create));
    41             document.Open();
    42             BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    43             Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 0, 0)); 
    44   
    45             //按设置的字体输出文本
    46             document.Add(new Paragraph(this.TextBox1.Text.ToString(), fontChinese));
    47             //输出图片到PDF文件
    48             iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(Server.MapPath("Images/gyl.jpg"));
    49             document.Add(jpeg01);
    50             iTextSharp.text.Image jpeg02 = iTextSharp.text.Image.GetInstance(Server.MapPath("Images/yy.jpg"));
    51             document.Add(jpeg02);
    52   
    53             PdfPTable table = new PdfPTable(datatable.Columns.Count);
    54   
    55             for (int i = 0; i < datatable.Rows.Count; i++)
    56             {
    57                 for (int j = 0; j < datatable.Columns.Count; j++)
    58                 {
    59                     table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), fontChinese));
    60                 }
    61             }
    62             document.Add(table);
    63   
    64             document.Close();
    65             Response.Write("<script>alert('导出成功!');</script>");
    66         }
    67         catch (DocumentException de)
    68         {
    69             Response.Write(de.ToString());
    70         }
    71     }
    72 }

    01 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    02   
    03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    04   
    05 <html xmlns="http://www.w3.org/1999/xhtml" >
    06 <head runat="server">
    07     <title>无标题页</title>
    08 </head>
    09 <body>
    10     <form id="form1" runat="server">
    11     <div>
    12         <br />
    13         请输入一些文字用于测试:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    14         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" /></div>
    15     </form>
    16 </body>
    17 </html>
  • 相关阅读:
    bzoj1797
    bzoj1266
    bzoj1497
    bzoj1412
    bzoj3156
    JSOI2014第三轮总结
    bzoj1855
    bzoj1044
    codeforces 371D
    codeforces 371B
  • 原文地址:https://www.cnblogs.com/bdzwater/p/2184754.html
Copyright © 2020-2023  润新知