asp.net2.0导出pdf文件完美解决方案[转载]

摘要:
越来越多的电子书、产品说明、公司公告、网络资料和电子邮件开始使用PDF文件。PDF格式文件已成为数字信息的事实上的工业标准。Adobe设计了PDF文件格式,以支持跨平台、多媒体集成的信息发布和发布,特别是为网络信息发布提供支持。PDF文件格式可以将独立于设备和分辨率的文本、字体、格式、颜色、图形和图像封装在一个文件中。在日常工作中,我们经常需要将报告和网页导出为PDF。

PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式。这种文件格式与操作系统平台无关,也就是说,PDF文件不管是在Windows,Unix还是在苹果公司的Mac OS操作系统中都是通用的。这一特点使它成为在Internet上进行电子文档发行和数字化信息传播的理想文档格式。越来越多的电子图书、产品说明、公司文告、网络资料、电子邮件开始使用PDF格式文件。PDF格式文件目前已成为数字化信息事实上的一个工业标准。

Adobe公司设计PDF文件格式的目的是为了支持跨平台上的,多媒体集成的信息出版和发布,尤其是提供对网络信息发布的支持。为了达到此目的, PDF具有许多其他电子文档格式无法相比的优点。PDF文件格式可以将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。该格式文件还可以包含超文本链接、声音和动态影像等电子信息,支持特长文件,集成度和安全可靠性都较高。

日常工作中经常遇到想把报表和网页导出到PDF的需求。本文提供完美的解决方案

ASP.NET导出到PDF最终效果图(其实winform和控制台程序都一样可以做)。

本文实现 文字图片数据表的导出
asp.net2.0导出pdf文件完美解决方案[转载]第1张
 核心技术方案:使用itextsharp.dll

1.下载itextsharp.dll和ICSharpCode.SharpZipLib.dll
http://sourceforge.net/project/showfiles.php?group_id=72954

asp.net2.0导出pdf文件完美解决方案[转载]第2张

iTextSharp.tutorial.01.zip    示例文件 提供了各种解决方案本文由于时间问题仅做抛砖引玉,希望大家自己研究其他需求

itextsharp.dll  itextsharp-4.0.3-dll.zip  

ICSharpCode.SharpZipLib.dll    http://download.csdn.net/down/135897  ICSharpCode.SharpZipLib.dll   

SharpZipLib.dll类库中的内容实现的压缩与解压功能,它是开源

2.引用itextsharp.dll和ICSharpCode.SharpZipLib.dll

asp.net2.0导出pdf文件完美解决方案[转载]第3张

3.后台代码:

asp.net2.0导出pdf文件完美解决方案[转载]第4张using System;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Data;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Configuration;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Web;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Web.Security;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Web.UI;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Web.UI.WebControls;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Web.UI.WebControls.WebParts;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.Web.UI.HtmlControls;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using iTextSharp;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using iTextSharp.text;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using iTextSharp.text.pdf;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
using System.IO;
asp.net2.0导出pdf文件完美解决方案[转载]第4张
public partial class _Default : System.Web.UI.Page 
asp.net2.0导出pdf文件完美解决方案[转载]第18张asp.net2.0导出pdf文件完美解决方案[转载]第19张
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张    
static DataTable datatable = new DataTable("testpdf");
asp.net2.0导出pdf文件完美解决方案[转载]第20张    
protected void Page_Load(object sender, EventArgs e)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张    
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张         
//判断是否是回发页面http://blog.csdn.net/21aspnet
asp.net2.0导出pdf文件完美解决方案[转载]第20张
        if (!Page.IsPostBack)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张        
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张         DataRow dr;
asp.net2.0导出pdf文件完美解决方案[转载]第20张        
//建立Column例,可以指明例的类型,这里用的是默认的string
asp.net2.0导出pdf文件完美解决方案[转载]第20张
        datatable.Columns.Add(new DataColumn("编号"));
asp.net2.0导出pdf文件完美解决方案[转载]第20张        datatable.Columns.Add(
new DataColumn("用户名"));
asp.net2.0导出pdf文件完美解决方案[转载]第20张        
for (int i = 1; i < 5; i++)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张        
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张            dr 
= datatable.NewRow();
asp.net2.0导出pdf文件完美解决方案[转载]第20张            dr[
0= System.Convert.ToString(i);
asp.net2.0导出pdf文件完美解决方案[转载]第20张            dr[
1= "清清月儿" + System.Convert.ToString(i);
asp.net2.0导出pdf文件完美解决方案[转载]第20张            datatable.Rows.Add(dr);
asp.net2.0导出pdf文件完美解决方案[转载]第39张        }

asp.net2.0导出pdf文件完美解决方案[转载]第39张        }

asp.net2.0导出pdf文件完美解决方案[转载]第20张        
asp.net2.0导出pdf文件完美解决方案[转载]第20张        
asp.net2.0导出pdf文件完美解决方案[转载]第39张    }

asp.net2.0导出pdf文件完美解决方案[转载]第20张    
protected void Button1_Click(object sender, EventArgs e)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张    
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张       
asp.net2.0导出pdf文件完美解决方案[转载]第20张
asp.net2.0导出pdf文件完美解决方案[转载]第20张        
try
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张        
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张            Document document 
= new Document();
asp.net2.0导出pdf文件完美解决方案[转载]第20张            PdfWriter.getInstance(document, 
new FileStream(Server.MapPath("Chap0101.pdf"), FileMode.Create));
asp.net2.0导出pdf文件完美解决方案[转载]第20张                        document.Open();
asp.net2.0导出pdf文件完美解决方案[转载]第20张            BaseFont bfChinese 
= BaseFont.createFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
asp.net2.0导出pdf文件完美解决方案[转载]第20张            Font fontChinese 
= new Font(bfChinese, 12, Font.NORMAL,new Color(0, 0, 0));
asp.net2.0导出pdf文件完美解决方案[转载]第20张
asp.net2.0导出pdf文件完美解决方案[转载]第20张                        document.Add(
new Paragraph(this.TextBox1.Text.ToString(), fontChinese));
asp.net2.0导出pdf文件完美解决方案[转载]第20张
asp.net2.0导出pdf文件完美解决方案[转载]第20张            iTextSharp.text.Image jpeg 
= iTextSharp.text.Image.getInstance(Server.MapPath("pic015.jpg"));
asp.net2.0导出pdf文件完美解决方案[转载]第20张            document.Add(jpeg);
asp.net2.0导出pdf文件完美解决方案[转载]第20张            PdfPTable table 
= new PdfPTable(datatable.Columns.Count);
asp.net2.0导出pdf文件完美解决方案[转载]第20张
asp.net2.0导出pdf文件完美解决方案[转载]第20张            
for (int i = 0; i < datatable.Rows.Count; i++)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张            
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张                
for (int j = 0; j < datatable.Columns.Count; j++)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张                
...{
asp.net2.0导出pdf文件完美解决方案[转载]第20张                    table.addCell(
new Phrase(datatable.Rows[i][j].ToString(), fontChinese));
asp.net2.0导出pdf文件完美解决方案[转载]第39张                }

asp.net2.0导出pdf文件完美解决方案[转载]第39张            }

asp.net2.0导出pdf文件完美解决方案[转载]第20张            document.Add(table);
asp.net2.0导出pdf文件完美解决方案[转载]第20张
asp.net2.0导出pdf文件完美解决方案[转载]第20张            document.Close();
asp.net2.0导出pdf文件完美解决方案[转载]第39张        }

asp.net2.0导出pdf文件完美解决方案[转载]第20张        
catch (DocumentException de)
asp.net2.0导出pdf文件完美解决方案[转载]第22张asp.net2.0导出pdf文件完美解决方案[转载]第23张        
...{;
asp.net2.0导出pdf文件完美解决方案[转载]第20张            Response.Write(de.ToString());
asp.net2.0导出pdf文件完美解决方案[转载]第39张        }

asp.net2.0导出pdf文件完美解决方案[转载]第39张    }

asp.net2.0导出pdf文件完美解决方案[转载]第83张}

asp.net2.0导出pdf文件完美解决方案[转载]第4张

4.前台代码:

asp.net2.0导出pdf文件完美解决方案[转载]第18张asp.net2.0导出pdf文件完美解决方案[转载]第19张<%...@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
asp.net2.0导出pdf文件完美解决方案[转载]第4张
asp.net2.0导出pdf文件完美解决方案[转载]第4张
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
asp.net2.0导出pdf文件完美解决方案[转载]第4张
asp.net2.0导出pdf文件完美解决方案[转载]第4张
<html xmlns="http://www.w3.org/1999/xhtml" >
asp.net2.0导出pdf文件完美解决方案[转载]第4张
<head runat="server">
asp.net2.0导出pdf文件完美解决方案[转载]第4张    
<title>清清月儿 制作导出PDF http://blog.csdn.net/21aspnet</title>
asp.net2.0导出pdf文件完美解决方案[转载]第4张
</head>
asp.net2.0导出pdf文件完美解决方案[转载]第4张
<body>
asp.net2.0导出pdf文件完美解决方案[转载]第4张    
<form id="form1" runat="server">
asp.net2.0导出pdf文件完美解决方案[转载]第4张    
<div>
asp.net2.0导出pdf文件完美解决方案[转载]第4张        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
asp.net2.0导出pdf文件完美解决方案[转载]第4张        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" /></div>
asp.net2.0导出pdf文件完美解决方案[转载]第4张    
</form>
asp.net2.0导出pdf文件完美解决方案[转载]第4张
</body>
asp.net2.0导出pdf文件完美解决方案[转载]第4张
</html>

5.前台操作:
asp.net2.0导出pdf文件完美解决方案[转载]第102张

 6.属性说明:

itextsharp-4.0.3-dll.zip   示例文件包含几乎所有的PDF处理需求

颜色:
 Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 0, 0)); //黑Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 255, 0)); //绿

注释
 
iText支持不同风格的注释。

u 文本注释:

你可以添加一小段文本到你的文档中,但它并非文档内容的一部分,注释有标题和内容:

Annotation a = new Annotation(

"authors",

"Maybe it's because I wanted to be an author myself that I wrote iText.");

对齐方式:
cell.HorizontalAlignment = Element.ALIGN_CENTER;

cell.VerticalAlignment = Element.ALIGN_MIDDLE;

下划线/删除线:
Chunk chunk1 = new Chunk("This text is underlined", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));

Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC | Font.STRIKETHRU));

加密:
public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions);

由于时间问题:更多如页眉页脚属性目录水印单元格间距边框等等请大家自己研究文档。

想得到想不到示例文件都有。

免责声明:文章转载自《asp.net2.0导出pdf文件完美解决方案[转载]》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇lucene.net 详解vim修改文件锁定处理办法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

texstudio设置外部浏览器及右侧预览不能使用问题

刚装的texstudio,今天不知什么原因右侧显示的pdf文件一直是以前的,百度了下没找到,自己摸索了下,只需要把构建里面pdf查看器更改下即可  如果想更改外部pdf查看器,只需要设置下命令里面外部pdf查看器即可 点此按钮,找到要设置软件exe文件即可...

攻防世界-crypto-banana-princess(ROT13加解密、pdftohtml)

附件是一个pdf文件,打开时报错,格式错误:不是一个PDF文件或该文件已损坏。 1.对比文件,发现是ROT13 用notepad++打开该文件,文件头为%CQS-1.5,打开一个正常pdf文件,文件头为%PDF-1.7。 这两个形式完全一样,数字可以理解为版本号。那就思路来了,估计是进行了位移?尝试后发现: 是对整个PDF文件进行了ROT13。 ROT1...

编辑DataTable的方法

原本以为DataTable编辑很简单,其实不简单。一、修改必须这样:DataRow row=myTable.Rows[0];row.BeginEdit();row.["userName"]="aa";row["pwd"]="121";row.EndEdit();实例:实现DataGridViewer数据绑定,并改变其中一列的数据格式//dg绑定数据    ...

从DataTable中取前几条数据

具体就拿一个简单的例子来说吧:第一次从数据库拿到一个DataTable后在后面的处理的过程中发现另外一个地方也要这个DataTable里面的部分数据,例如说要这个DataTable中的前10条吧,我又不想从数据库里面再去读取数据,想办法从这个DataTable中拿出来,常用的可能就是循环一条条读取,但是我还想走捷径呢,google里面找了下,也没有发现什么...

戳进来呗!五分钟就学会Python3怎么修改Excel数据(xlwt使用)

上次我们看过了xlrd的使用方法,那么怎么对Excel执行修改操作的?对于旧版的Excel文件我们用xlwt,但是对于新版的xlsx文件,使用openpyxl更合适一些。xlwt的使用比较简单,我们赶快开始学习吧 安装 安装和xlrd一样,pip install xlwt 创建对象 workbook = xlwt.Workbook() 创建表单对象 我们...

将dataGridView数据转成DataTable

如已绑定过数据源: DataTable dt = (dataGridView1.DataSource as DataTable);如未绑定过数据源: public DataTable GetDgvToTable(DataGridView dgv) { DataTable dt = new DataTable();...