当前位置: 编程技术>.net/c#/asp.net
asp.net写入pdf文件的实现代码
来源: 互联网 发布时间:2014-08-30
本文导语: 1,前台代码部分 代码示例: 制作导出PDF_www. 2,后台代码部分 代码示例: using...
1,前台代码部分
代码示例:
制作导出PDF_www.
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());
}
}
}
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());
}
}
}