在Asp.Net中操作PDF – iTextSharp-列表
來自: http://blog.csdn.net//chenguang79/article/details/41478269
ListItem li = new ListItem();
轉自:http://www.cnblogs.com/CareySon/archive/2011/11/04/2235834.html
在前文中,我們已經知道了如何利用iTextSharp創建PDF文檔,設置字體樣式和風格.本文開始講述iTextSharp中的有序列表和無需列表
在iTextSharp中列表的創建是通過iTextSharp.text.List對象實現的。列表實質上是iTextSharp.text.ListItem的集合.也就是由ListItem組成的數組.ListItem繼承了Paragraph對象(而Paragraph對象繼承于Phrase,Phrase又繼承于Arraylist),所以生成的每一個List都會自動換行.就如同List在HTML分為<ul>和<ol>一樣,iTextSharp中列表同樣分為有序列表和無序列表.下面我們來直接看如何生成列表的代碼:
<span style="color:#0000ff;">string</span> path = Server.MapPath("<span style="color:#8b0000;">PDFs</span>"); it.Document doc = <span style="color:#0000ff;">new</span> it.Document(); <span style="color:#0000ff;">try</span> { PdfWriter.GetInstance(doc, <span style="color:#0000ff;">new</span> FileStream(path + "<span style="color:#8b0000;">/Lists.pdf</span>", FileMode.Create)); doc.Open(); it.List list = <span style="color:#0000ff;">new</span> it.List(it.List.UNORDERED); list.Add(<span style="color:#0000ff;">new</span> it.ListItem("<span style="color:#8b0000;">One</span>")); list.Add("<span style="color:#8b0000;">Two</span>"); list.Add("<span style="color:#8b0000;">Three</span>"); list.Add("<span style="color:#8b0000;">Four</span>"); list.Add("<span style="color:#8b0000;">Five</span>"); it.Paragraph paragraph = <span style="color:#0000ff;">new</span> it.Paragraph(); <span style="color:#0000ff;">string</span> text = "<span style="color:#8b0000;">Lists</span>"; paragraph.Add(text); doc.Add(paragraph); doc.Add(list); } <span style="color:#0000ff;">catch</span> (it.DocumentException dex) { Response.Write(dex.Message); } <span style="color:#0000ff;">catch</span> (IOException ioex) { Response.Write(ioex.Message); } <span style="color:#0000ff;">finally</span> { doc.Close(); }如果你對上面代碼的意思并不了解.那么為什么要用”it"引用List的確需要解釋一下.正如代碼所示,it作為引用某些類,因為如果你直接在ASP.Net code-behind模式下工作,你會發現visual studio在引用iTextSharp的ListItem時和也包含ListItem的System.Web.UI.WebControls發生命名空間沖突.這意味著如果僅僅是用如下代碼:
ListItem li = new ListItem();
則會報不明確引用的警告。解決方法是使用完全引用:
iTextSharp.text.ListItem li = new iTextSharp.text.ListItem(); 但是使用完全引用又臭又長,所以這里使用了簡潔引用: <pre name="code" class="csharp">using it = iTextSharp.text;
現在,你就可以使用別名了.
回到講述我們實際代碼的作用,第一件事是創建一個List對象,并傳入一個布爾類型的參數告訴List生成的是有序或無序列表.默認是False(也就是無序列表),然后為List加入了5個項。第一個項是通過匿名函數傳入String參數類型來創建ListItem并傳入,從第二個開始,則是直接傳入String類型的參數.最后是創建一個Paragraph對象和list對象共同傳入document.
如上圖所見,每一個列表項都像Paragraph那樣自己單占一行.還有列表是無序列表,每一個列表項之前都用一個橫杠作為修飾,并且列表沒有縮進。但iTextSharp提供了多種方法允許設置列表使其更加美觀:it.List list = new it.List(it.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); list.IndentationLeft = 30f;
上面第二個參數(float類型)傳入List的構造函數,用于將每一個列表項的縮進設置成10(也就是列表符號和列表項第一個字符的距離。).然后我通過SetListSymbol方法將列表項符號改成更傳統的”.”,最后我將整個列表向右縮進30,現在列表看起來就好多了:
如果你使用有序列表并將羅馬數字作為標識,你可以使用RomanList類:
RomanList romanlist = new RomanList(true, 20); romanlist.IndentationLeft = 30f; romanlist.Add("One"); romanlist.Add("Two"); romanlist.Add("Three"); romanlist.Add("Four"); romanlist.Add("Five"); doc.Add(romanlist);
由于某些奇怪的理由,傳入RomanList構造函數的第二個參數是一個Int類型的值,第一個參數告訴RomanList究竟使用大寫還是小寫作為行項目標識:
還有一個GreekList類支持使用希臘字符作為列表項目的標識,還有其它兩個類ZapfDingbatsList 和ZapfDingbatsNumberList,由于他們使用了ZapfDingBats字體,所以這兩個類對列表項符號提供了更多豐富的選項,希臘和羅馬字符作為行項目標識時,分別不能超過24和26個行項目,而ZapfDingBatsNumberList最多只能處理10個字符,當字符超出范圍后,列表又會從0開始.
ZapfDingbatsList zlist = new it.ZapfDingbatsList(49, 15); zlist.Add("One"); zlist.Add("Two"); zlist.Add("Three"); zlist.Add("Four"); zlist.Add("Five"); doc.Add(zlist);
列表之間還可以相互嵌套,因為List.Add()方法接受一個Object類型的參數,所以你只要傳入一個有效的List對象就行。下面代碼首先創建了一個RomanList對象,然后再創建一個有序列表.我們將RomanList對象添加到有序列表上,則RomanList會相對于父有序列表自動向后縮進:
RomanList romanlist = new RomanList(true, 20); romanlist.IndentationLeft = 10f; romanlist.Add("One"); romanlist.Add("Two"); romanlist.Add("Three"); romanlist.Add("Four"); romanlist.Add("Five"); List list = new List(List.ORDERED, 20f); list.SetListSymbol("\u2022"); list.IndentationLeft = 20f; list.Add("One"); list.Add("Two"); list.Add("Three"); list.Add("Roman List"); list.Add(romanlist); list.Add("Four"); list.Add("Five"); doc.Add(paragraph); doc.Add(list);
