如何使用iTextSharp将支持中文显示的ASP.NET GridView导出为PDF文件?

2026-03-30 13:281阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计331个文字,预计阅读时间需要2分钟。

如何使用iTextSharp将支持中文显示的ASP.NET GridView导出为PDF文件?

摘要:使用ASP.NET和iTextSharp组件将GridView转换为PDF,今天有空刚把USER需求记录起来,这次要实现将GridView转换成PDF文档,利用iTextSharp组件来操作。链接:http://sourceforge.net/projects/itextsharp/files/iTextSharp/iTextSharp-5.0/

如何使用iTextSharp将支持中文显示的ASP.NET GridView导出为PDF文件?

摘要:[ASP.NET] 利用itextsharp组件把GridView变成PDF


今天有空刚好把USER需求记录起来

这次要把GridView转成PDF档

利用itextsharp组件来作sourceforge.net/projects/itextsharp/files/itextsharp/iTextSharp-5.0.4/itextsharp-5.0.4-dll.zip/download

不多说直接看CODE

GridView导出PDF


后端程序:

protected void Button1_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("content-disposition", "attachment;filename=Y2J.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringReader reader = new StringReader(stringWrite.ToString()); Document doc = new Document(PageSize.A4); PdfWriter.GetInstance(doc, Response.OutputStream); //设定中文 BaseFont BaseF = BaseFont.CreateFont("C:\Windows\Fonts\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font fontCh = new Font(BaseF, 14); doc.Open(); int gvRowsCount = GridView1.Rows[0].Cells.Count; PdfPTable ptb = new PdfPTable(gvRowsCount); //表格标题 for (int h = 0; h < gvRowsCount; h++) { ptb.AddCell(new Phrase(GridView1.HeaderRow.Cells[h].Text, fontCh)); } ptb.HeaderRows = 1; //表格内文 for (int i = 0; i < GridView1.Rows.Count; i++) { for (int j = 0; j < gvRowsCount; j++) { ptb.AddCell(new Phrase(GridView1.Rows[i].Cells[j].Text, fontCh)); } } //写入并关闭 doc.Add(ptb); doc.Close(); }







Y2J‘s Life:kimenyeh.blogspot.tw/

原文:大专栏 [ASP.NET] iTextSharp产生PDF档...Part_1(把GridView变成PDF(支持中文显示))

本文共计331个文字,预计阅读时间需要2分钟。

如何使用iTextSharp将支持中文显示的ASP.NET GridView导出为PDF文件?

摘要:使用ASP.NET和iTextSharp组件将GridView转换为PDF,今天有空刚把USER需求记录起来,这次要实现将GridView转换成PDF文档,利用iTextSharp组件来操作。链接:http://sourceforge.net/projects/itextsharp/files/iTextSharp/iTextSharp-5.0/

如何使用iTextSharp将支持中文显示的ASP.NET GridView导出为PDF文件?

摘要:[ASP.NET] 利用itextsharp组件把GridView变成PDF


今天有空刚好把USER需求记录起来

这次要把GridView转成PDF档

利用itextsharp组件来作sourceforge.net/projects/itextsharp/files/itextsharp/iTextSharp-5.0.4/itextsharp-5.0.4-dll.zip/download

不多说直接看CODE

GridView导出PDF


后端程序:

protected void Button1_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("content-disposition", "attachment;filename=Y2J.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringReader reader = new StringReader(stringWrite.ToString()); Document doc = new Document(PageSize.A4); PdfWriter.GetInstance(doc, Response.OutputStream); //设定中文 BaseFont BaseF = BaseFont.CreateFont("C:\Windows\Fonts\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font fontCh = new Font(BaseF, 14); doc.Open(); int gvRowsCount = GridView1.Rows[0].Cells.Count; PdfPTable ptb = new PdfPTable(gvRowsCount); //表格标题 for (int h = 0; h < gvRowsCount; h++) { ptb.AddCell(new Phrase(GridView1.HeaderRow.Cells[h].Text, fontCh)); } ptb.HeaderRows = 1; //表格内文 for (int i = 0; i < GridView1.Rows.Count; i++) { for (int j = 0; j < gvRowsCount; j++) { ptb.AddCell(new Phrase(GridView1.Rows[i].Cells[j].Text, fontCh)); } } //写入并关闭 doc.Add(ptb); doc.Close(); }







Y2J‘s Life:kimenyeh.blogspot.tw/

原文:大专栏 [ASP.NET] iTextSharp产生PDF档...Part_1(把GridView变成PDF(支持中文显示))