首页 > 编程 > C# > 正文

C#实现pdf导出 .Net导出pdf文件

2024-09-07 17:05:35
字体:
来源:转载
供稿:网友

最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码、可以成功的实现导出pdf文件。 

在编码前需要在网上下载个itextsharp.dll,此程序集是必备的。楼主下载的是5.0版本,之前下了个5.4的似乎不好用。 

下载之后直接添加引用。 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Webpdf.aspx.cs" Inherits="Web导出pdf.Webpdf" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title></title></head><body>  <form id="form1" runat="server">    <div>      <br />      输入文字测试:<input type="text" name="name" value="" runat="server" id="txtpdf"/>            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />    </div>  </form></body></html>//上述代码乃前台页面,两个控件即可。下面即对应的后台代码using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf;using System.IO;namespace Web导出pdf{  public partial class Webpdf : System.Web.UI.Page  {    static DataTable datatable = new DataTable("testpdf");    protected void Page_Load(object sender, EventArgs e)    {      if (!IsPostBack)      {        DataRow dr=null;        //建立Column例,可以指明例的类型,这里用的是默认的string         datatable.Columns.Add(new DataColumn("编号"));        datatable.Columns.Add(new DataColumn("文件名"));        for (int i = 0; i < 5; i++)        {          dr = datatable.NewRow();          dr[0] = System.Convert.ToString(i);          dr[1] = "测试导出pdf文件" + 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);        iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL,new BaseColor(0,0,0));        //所输的内容按设置的字体到PDF文件        document.Add(new Paragraph(this.txtpdf.Value.ToString(), fontChinese));        //输出图片到PDF文件         iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(Server.MapPath("Image/001.png"));        document.Add(jpeg01);            iTextSharp.text.Image jpeg02 = iTextSharp.text.Image.GetInstance(Server.MapPath("Image/userIcon-1.png"));        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 (Exception ex)      {        Response.Write(ex.ToString());      }    }  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表