首页 技术 正文
技术 2022年11月13日
0 收藏 977 点赞 4,315 浏览 3308 个字

asp.net2.0导出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文件完美解决方案【转载】

核心技术方案:使用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.csdn.net/down/135897  ICSharpCode.SharpZipLib.dll   

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

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

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

3.后台代码:

asp.net2.0导出pdf文件完美解决方案【转载】; 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文件完美解决方案【转载】}

4.前台代码:

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.csdn.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 , 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);

接下来就要生成一个个的单元格并加入到表格中了。下面就是生成一个文字单元格的代码:

Phrase text1 = new Phrase(text,font); PdfPCell cell = new PdfPCell(text1); cell.VerticalAlignment = VA; cell.HorizontalAlignment = HA; cell.Padding = 0.5F; //cell.FixedHeight = height; cell.MinimumHeight = height;

要注意的是注释掉的一句,它如果被取消注释了,那么就意味着,如果单元格不足以容下其内容,也不会换行,不够显示的内容就被隐藏了。

如果想在单元格中放一个图片,可以像下面这样做:

iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(img,iTextSharp.text.BaseColor.WHITE); if (img.Width>cell.Width || img.Height>cell.Height) { img1.ScaleToFit(cell.Width, cell.Height); } cell.FixedHeight = cell.MinimumHeight; cell.Image = img1;

GetInstance 的 img 参数是 System.Drawing 中的 Image。如果你不希望单元格被撑大,就要设置其 FixedHeigh。如果还想看整个图片,就需要调用 ScaleToFit 方法。

有了 cell,直接调用PdfPTable.AddCell 方法就可以了。

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,960
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,484
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,745
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,779