asp.net2.0导出pdf文件完美解决方案【月儿原创】
asp.net2.0導(dǎo)出pdf文件完美解決方案
作者:清清月兒
主頁:http://blog.csdn.net/21aspnet/?????????? 時(shí)間:2007.5.28
PDF簡(jiǎn)介:PDF(Portable Document Format)文件格式是Adobe公司開發(fā)的電子文件格式。這種文件格式與操作系統(tǒng)平臺(tái)無關(guān),也就是說,PDF文件不管是在Windows,Unix還是在蘋果公司的Mac OS操作系統(tǒng)中都是通用的。這一特點(diǎn)使它成為在Internet上進(jìn)行電子文檔發(fā)行和數(shù)字化信息傳播的理想文檔格式。越來越多的電子圖書、產(chǎn)品說明、公司文告、網(wǎng)絡(luò)資料、電子郵件開始使用PDF格式文件。PDF格式文件目前已成為數(shù)字化信息事實(shí)上的一個(gè)工業(yè)標(biāo)準(zhǔn)。
Adobe公司設(shè)計(jì)PDF文件格式的目的是為了支持跨平臺(tái)上的,多媒體集成的信息出版和發(fā)布,尤其是提供對(duì)網(wǎng)絡(luò)信息發(fā)布的支持。為了達(dá)到此目的, PDF具有許多其他電子文檔格式無法相比的優(yōu)點(diǎn)。PDF文件格式可以將文字、字型、格式、顏色及獨(dú)立于設(shè)備和分辨率的圖形圖像等封裝在一個(gè)文件中。該格式文件還可以包含超文本鏈接、聲音和動(dòng)態(tài)影像等電子信息,支持特長(zhǎng)文件,集成度和安全可靠性都較高。
日常工作中經(jīng)常遇到想把報(bào)表和網(wǎng)頁導(dǎo)出到PDF的需求。本文提供完美的解決方案:
ASP.NET導(dǎo)出到PDF的最終效果圖(其實(shí)winform和控制臺(tái)程序都一樣可以做)。
本文實(shí)現(xiàn) 文字,圖片,數(shù)據(jù)表的導(dǎo)出
?核心技術(shù)方案:使用itextsharp.dll
?
1.下載itextsharp.dll和ICSharpCode.SharpZipLib.dll
http://sourceforge.net/project/showfiles.php?group_id=72954
iTextSharp.tutorial.01.zip??? 示例文件 提供了各種解決方案本文由于時(shí)間問題僅做拋磚引玉,希望大家自己研究其他需求
itextsharp.dll? itextsharp-4.0.3-dll.zip??
ICSharpCode.SharpZipLib.dll??? http://download.csdn.net/down/135897? ICSharpCode.SharpZipLib.dll???
SharpZipLib.dll類庫中的內(nèi)容實(shí)現(xiàn)的壓縮與解壓功能,它是開源的
2.引用itextsharp.dll和ICSharpCode.SharpZipLib.dll
3.后臺(tái)代碼:
?
using?System;using?System.Data;
using?System.Configuration;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?iTextSharp;
using?iTextSharp.text;
using?iTextSharp.text.pdf;
using?System.IO;
public?partial?class?_Default?:?System.Web.UI.Page?
...{
????static?DataTable?datatable?=?new?DataTable("testpdf");
????protected?void?Page_Load(object?sender,?EventArgs?e)
????...{
?????????//判斷是否是回發(fā)頁面http://blog.csdn.net/21aspnet
????????if?(!Page.IsPostBack)
????????...{
?????????DataRow?dr;
????????//建立Column例,可以指明例的類型,這里用的是默認(rèn)的string
????????datatable.Columns.Add(new?DataColumn("編號(hào)"));
????????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("Chap0101.pdf"),?FileMode.Create));
????????????????????????document.Open();
????????????BaseFont?bfChinese?=?BaseFont.createFont("C://WINDOWS//Fonts//simsun.ttc,1",?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("pic015.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));
????????????????}
????????????}
????????????document.Add(table);
????????????document.Close();
????????}
????????catch?(DocumentException?de)
????????...{;
????????????Response.Write(de.ToString());
????????}
????}
}
?
4.前臺(tái)代碼:
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?runat="server">
????<title>清清月兒?制作導(dǎo)出PDF?http://blog.csdn.net/21aspnet</title>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
????????<asp:TextBox?ID="TextBox1"?runat="server"></asp:TextBox>
????????<asp:Button?ID="Button1"?runat="server"?OnClick="Button1_Click"?Text="導(dǎo)出"?/></div>
????</form>
</body>
</html>
5.前臺(tái)操作:
?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支持不同風(fēng)格的注釋。
u 文本注釋:
你可以添加一小段文本到你的文檔中,但它并非文檔內(nèi)容的一部分,注釋有標(biāo)題和內(nèi)容:
Annotation a = new Annotation(
"authors",
"Maybe it's because I wanted to be an author myself that I wrote iText.");
對(duì)齊方式:
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);
由于時(shí)間問題:更多如頁眉頁腳屬性目錄水印單元格間距邊框等等請(qǐng)大家自己研究文檔。
你想得到的想不到的示例文件都有。
?
總結(jié)
以上是生活随笔為你收集整理的asp.net2.0导出pdf文件完美解决方案【月儿原创】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 别墅装修多少钱啊?
- 下一篇: asp.net的Ajax学习进阶