asp net2 0导出pdf文件完美解决方案【月儿原创】

               

asp.net2.0导出pdf文件完美解决方案

作者:清清月儿

主页:http://blog.****.net/21aspnet/           时间:2007.5.28

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文件完美解决方案【月儿原创】
 核心技术方案:使用itextsharp.dll

 

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

asp net2 0导出pdf文件完美解决方案【月儿原创】

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

itextsharp.dll  itextsharp-4.0.3-dll.zip  

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

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

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

asp net2 0导出pdf文件完美解决方案【月儿原创】

3.后台代码:

 

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

asp net2 0导出pdf文件完美解决方案【月儿原创】        }

asp net2 0导出pdf文件完美解决方案【月儿原创】        
asp net2 0导出pdf文件完美解决方案【月儿原创】        
asp net2 0导出pdf文件完美解决方案【月儿原创】    }

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

asp net2 0导出pdf文件完美解决方案【月儿原创】            }

asp net2 0导出pdf文件完美解决方案【月儿原创】            document.Add(table);
asp net2 0导出pdf文件完美解决方案【月儿原创】
asp net2 0导出pdf文件完美解决方案【月儿原创】            document.Close();
asp net2 0导出pdf文件完美解决方案【月儿原创】        }

asp net2 0导出pdf文件完美解决方案【月儿原创】        
catch (DocumentException de)
asp net2 0导出pdf文件完美解决方案【月儿原创】asp net2 0导出pdf文件完美解决方案【月儿原创】        
...{;
asp net2 0导出pdf文件完美解决方案【月儿原创】            Response.Write(de.ToString());
asp net2 0导出pdf文件完美解决方案【月儿原创】        }

asp net2 0导出pdf文件完美解决方案【月儿原创】    }

asp net2 0导出pdf文件完美解决方案【月儿原创】}

asp net2 0导出pdf文件完美解决方案【月儿原创】

 

4.前台代码:

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

5.前台操作:
asp net2 0导出pdf文件完美解决方案【月儿原创】

 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);

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

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

 

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.****.net/jiangjunshow