在Asp.Net中操作PDF – iTextSharp-列表

mrvm6024 8年前

来自: 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.

    1

    如上图所见,每一个列表项都像Paragraph那样自己单占一行.还有列表是无序列表,每一个列表项之前都用一个横杠作为修饰,并且列表没有缩进。但iTextSharp提供了多种方法允许设置列表使其更加美观:
it.List list = new it.List(it.List.UNORDERED, 10f);     list.SetListSymbol("\u2022");     list.IndentationLeft = 30f;

上面第二个参数(float类型)传入List的构造函数,用于将每一个列表项的缩进设置成10(也就是列表符号和列表项第一个字符的距离。).然后我通过SetListSymbol方法将列表项符号改成更传统的”.”,最后我将整个列表向右缩进30,现在列表看起来就好多了:

      2

 

   如果你使用有序列表并将罗马数字作为标识,你可以使用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究竟使用大写还是小写作为行项目标识:

 

    3

 

     还有一个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);

   4

 

    列表之间还可以相互嵌套,因为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);

     5