原文 [ASP.NET]以iTextSharp手繪表格並產生PDF下載
大家使用iTextSharp的機緣都不太一樣, 由於單位Crystal Report的License數量有限主管要我去找一個免費產生PDF方法, 避免Crystal Report的License使用過多, 於是乎就找上了iTextSharp.
Crystal Report 與 iTextSharp比較表
Crystal Report | iTextSharp | |
視覺化界面 | YES | NO |
轉換成PDF | YES | YES |
費用 | YES | NO |
第一次總是特別辛苦的, 為了畫出一張收據, 沒有視覺化界面的幫忙, 透過大腦的想像, 終於完成了任務, 但是對於使用Crystal Report的老手們, 應該相當不習慣, 因為看不見畫的東西, 對他們是不方便的, 但可以省錢, 我想老闆不會說不, 最後我把我學習的成果做成一個範例.
請記得先去網站下載iTextSharp.dll這個檔案加入參考
01 |
using System; |
02 |
using System.Collections.Generic; |
03 |
using System.Linq; |
04 |
using System.Web; |
05 |
using System.Web.UI; |
06 |
using System.Web.UI.WebControls; |
07 |
using System.IO; |
08 |
using iTextSharp.text; |
09 |
using iTextSharp.text.pdf; |
10 |
11 |
public partial class _Default : System.Web.UI.Page |
12 |
{ |
13 |
protected void Page_Load( object sender, EventArgs e) |
14 |
{ |
15 |
//文件初始化 |
16 |
Document Doc= new Document(); |
17 |
18 |
MemoryStream Memory= new MemoryStream(); |
19 |
20 |
PdfWriter PdfWriter = PdfWriter.GetInstance(Doc, Memory); |
21 |
22 |
//字型設定 |
23 |
string FontPath = Server.MapPath( "kaiu.ttf" ); |
24 |
25 |
BaseFont bfChinese = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); |
26 |
27 |
Font ChFont = new Font(bfChinese, 9); |
28 |
29 |
//文件開啟 |
30 |
Doc.Open(); |
31 |
32 |
//設定表格需要幾攔幾列 (如果不設列,可能會在使用RowSpan或ColSpan發生錯誤) |
33 |
iTextSharp.text.Table Tb = new iTextSharp.text.Table(4,5); |
34 |
//設定表格的Padding |
35 |
Tb.Padding = 4; |
36 |
//自動填滿欄位(如果沒有填滿欄位,不會畫出欄位的線條) |
37 |
Tb.AutoFillEmptyCells = true ; |
38 |
39 |
//抬頭 |
40 |
Paragraph Title = new Paragraph( "iTextSharp畫表格測試" , ChFont); |
41 |
//內容水平置中 |
42 |
Title.SetAlignment( "center" ); |
43 |
44 |
//Cell內容 |
45 |
iTextSharp.text.Cell Tc; |
46 |
Tc= new iTextSharp.text.Cell( new Phrase( "點部落" ,ChFont)); |
47 |
//內容水平置中 |
48 |
Tc.HorizontalAlignment = Element.ALIGN_CENTER; |
49 |
//內容高度置中 (Top,Middle感覺不到有沒有移動) |
50 |
Tc.VerticalAlignment = Element.ALIGN_TOP; |
51 |
Tc.Rowspan = 2; |
52 |
Tc.Colspan = 2; |
53 |
|
54 |
//將Cell加入表格 |
55 |
Tb.AddCell(Tc); |
56 |
57 |
Tc = new iTextSharp.text.Cell( new Phrase( "www.dotblogs.com.tw" , ChFont)); |
58 |
//內容水平置中 |
59 |
Tc.HorizontalAlignment = Element.ALIGN_CENTER; |
60 |
//內容高度置中(Top,Middle感覺不到有沒有移動) |
61 |
Tc.VerticalAlignment = Element.ALIGN_TOP; |
62 |
Tc.Rowspan = 2; |
63 |
Tc.Colspan = 2; |
64 |
65 |
Tb.AddCell(Tc); |
66 |
|
67 |
//加入抬頭 |
68 |
Doc.Add(Title); |
69 |
//把表格加入文件 |
70 |
Doc.Add(Tb); |
71 |
72 |
//開啟新的一頁 |
73 |
Doc.NewPage(); |
74 |
75 |
//加入抬頭 |
76 |
Doc.Add(Title); |
77 |
//把表格加入文件 |
78 |
Doc.Add(Tb); |
79 |
80 |
//文件關閉 |
81 |
Doc.Close(); |
82 |
83 |
84 |
//檔案下載 |
85 |
Response.Clear(); |
86 |
Response.AddHeader( "Content-Disposition" , "attachment; filename=pdfExample.pdf" ); |
87 |
Response.ContentType = "application/octet-stream" ; |
88 |
89 |
Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length); |
90 |
Response.OutputStream.Flush(); |
91 |
Response.OutputStream.Close(); |
92 |
Response.Flush(); |
93 |
Response.End(); |
94 |
95 |
} |
96 |
} |