当前位置: 代码迷 >> 综合 >> 一步一步 IText.Sharp Chunk Phrase Paragraph List使用
  详细解决方案

一步一步 IText.Sharp Chunk Phrase Paragraph List使用

热度:93   发布时间:2023-12-14 03:10:16.0

上面两篇介绍了PDF文档的创建和中文支持设置方法,下面对文档经常使用的对象时行介绍:

块(Chunk)、短句(Phrase)、段落(Paragraph)、列表(List)

文档中的对象UML图,如下


一步一步 IText.Sharp Chunk Phrase Paragraph List使用

上面两篇介绍了PDF文档的创建和中文支持设置方法,下面对文档经常使用的对象时行介绍:

块(Chunk)、短句(Phrase)、段落(Paragraph)、列表(List)

文档中的对象UML图,如下:

一、块(Chunk)

块(Chunk)是能被添加到文档的文本的最小单位,块可以用于构建其他基础元素如短句、段落、锚点等,块是一个有确定字体的字符串,要添加块到文档中时,其他所有布局变量均要被定义。

它有字体、大小、颜色、粗体,背景色、下划线,删除线等属性。

示例代码:

   1: static  void RegisterFont()
   2:         {
     
   3:             BaseFont.AddToResourceSearch("iTextAsian.dll");
   4:             BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
   5:             FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
   6:                                  @"\..\Fonts\STSONG.ttf");
   7:             FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
   8:                                  @"\..\Fonts\simhei.ttf");
   9:             FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
  10:                                  @"\..\Fonts\simsun.ttc");
  11:         }
  12:  
  13:         static void Main(string[] args)
  14:         {
     
  15:             RegisterFont();
  16:             Document document = new Document();
  17:             PdfWriter.GetInstance(document, new FileStream(string.Format("{0}helloworld.pdf",
  18:                 AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
  19:               .InitialLeading = 16;
  20:             document.Open();
  21:           
  22:             string[] contries = new string[]{
     "美国","英甲","中国","朝鲜","日本"};
  23:             for (int index = 1; index <= contries.Length;index++ )
  24:             {
     
  25:                 var contry = contries[index-1];
  26:                 document.Add(new Chunk(contry,FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 16)));
  27:                 document.Add(new Chunk(" "));
  28:                 Font font = new Font(
  29:                   Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
  30:                 Chunk id = new Chunk(index.ToString(), font);
  31:                 id.SetBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
  32:                 id.SetTextRise(6);
  33:                 document.Add(id);
  34:                 document.Add(Chunk.NEWLINE);
  35:             }
  36:            
  37:             document.Close();
  38:         }

执行结果如下:

在上面的代码中,使用设置行距和使用一个特殊的块:Chunk.NEWLINE

作为示例,可以不设置行距或设置为0,当为零时,多行就会显示到一行上面(重叠)

当设置为0 时,根本就看不清楚。

在创建对象时,如果没有指定字体及其大小,默认情况下是Helvetica,12pt,并且这个设置是没有办法修改的。只有在创建对象时显示指定字体。

SetBackground用于设置块的背景色,SetTextRise是设置上标,其中参数表示,离开基线的距离,如果设置负数就表示设置下标。

二、语句(Phrase)

短句(Phrase)是一系列以特定间距(两行之间的距离)作为参数的块,一个短句有一个主字体,但短句中的一些块具有不同于主字体的字体,你有更多的选择去创建短句。

简单来说,就是由多个同一行间距块组成。

演示代码,如下:

   1: static void Main(string[] args)
   2:         {
     
   3:             RegisterFont();
   4:             BOLD_UNDERLINED = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
   5:             NORMAL = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12);
   6:             Document document = new Document();
   7:             PdfWriter.GetInstance(document, new FileStream(string.Format("{0}helloworld.pdf",
   8:                 AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
   9:               .InitialLeading = 30;
  10:             document.Open();
  11:  
  12:             string[] contries = new string[] { "美国", "英甲", "中国", "朝鲜", "日本" };
  13:             for (int index = 1; index <= contries.Length; index++)
  14:             {
     
  15:                 var contry = contries[index - 1];
  16:                 document.Add(CreateDirectorPhrase(contry));
  17:                 document.Add(Chunk.NEWLINE);
  18:             }
  19:  
  20:             document.Close();
  21:         }
  22:  
  23:         public static Font BOLD_UNDERLINED = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
  24:         public static Font NORMAL = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12);
  25:         public static Phrase CreateDirectorPhrase(string str)
  26:         {
     
  27:             Phrase director = new Phrase();
  28:             director.Add(new Chunk(str, BOLD_UNDERLINED));
  29:             director.Add(new Chunk(",", BOLD_UNDERLINED));
  30:             director.Add(new Chunk(" ", NORMAL));
  31:             director.Add(new Chunk(str, NORMAL));
  32:             return director;
  33:         }

执行结果:

默认情况下,行间距是1.5倍字体大小,可以不用设置行间距,就比较好的显示文档。

三、段落(Paragraph)

段落是一系列块和(或)短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。

说明:一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距。

更改分割符

通常,当文本不能放在一行时,文本将被分割成不同的部分,iText首先会查找分割符,如果没有找到,文本将在行尾被截断。有一些预定的分割符如“ ”空格和“-”连字符,但是你可以使用setSplitCharacter方法来覆盖这些默认值。

以使用IndentationLeft和IndentationRight,FirstLineIndent属性设置缩排;

演示代码如下:

   1: public static Font BOLD_UNDERLINED = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
   2:         public static Font NORMAL = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12);
   3:         static Font BOLDITALIC = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.ITALIC);
   4:         public static Phrase CreateDirectorPhrase(string str)
   5:         {
     
   6:             Phrase director = new Phrase();
   7:             //director.Add(new Chunk(str, BOLD_UNDERLINED));
   8:             //director.Add(new Chunk(",", BOLD_UNDERLINED));
   9:             //director.Add(new Chunk(" ", NORMAL));
  10:             director.Add(new Chunk(str, NORMAL));
  11:             return director;
  12:         }
  13:  
  14:         static void Main(string[] args)
  15:         {
     
  16:             RegisterFont();
  17:             BOLD_UNDERLINED = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
  18:             NORMAL = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12);
  19:             BOLDITALIC = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.ITALIC);
  20:             Document document = new Document();
  21:             PdfWriter.GetInstance(document, new FileStream(string.Format("{0}helloworld.pdf",
  22:                 AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
  23:               .InitialLeading = 30;
  24:             document.Open();
  25:  
  26:             string[] contries = new string[] { "美国", "英甲", "中国", "朝鲜", "日本" };
  27:             List<Movie> list = new List<Movie>() { new Movie() { Countries = new List<string>() { "中国", "美国" }, Directors = new List<string>() { "张三", "李四" }, Duration = "120", OriginalTitle = "电影一", Year = "2010" }, new Movie() { Countries = new List<string>() { "英国", "美国" }, Directors = new List<string>() { "张三", "王五" }, Duration = "80", OriginalTitle = "电影二", Year = "2011" }, new Movie() { Countries = new List<string>() { "英国", "中国" }, Directors = new List<string>() { "王五", "张三" }, Duration = "200", OriginalTitle = "2012", Year = "2009" }, new Movie() { Countries = new List<string>() { "日本", "中国" }, Directors = new List<string>() { "王五", "李四" }, Duration = "180", OriginalTitle = "电影四", Year = "2009" } };
  28:             for (int index = 1; index <= list.Count; index++)
  29:             {
     
  30:                 var contry = list[index - 1];
  31:                 document.Add(CreateMovieInformation(contry));
  32:                 document.Add(Chunk.NEWLINE);
  33:             }
  34:  
  35:             document.Close();
  36:         }
  37:  
  38:         public static Paragraph CreateYearAndDuration(Movie movie)
  39:         {
     
  40:             Paragraph info = new Paragraph();
  41:             info.Font = (NORMAL);
  42:             info.Add(new Chunk("年代: ", BOLDITALIC));
  43:             info.Add(new Chunk(movie.Year,
  44:               NORMAL));
  45:             info.Add(new Chunk("持续时间: ", BOLDITALIC));
  46:             info.Add(new Chunk(movie.Duration,
  47:               NORMAL));
  48:             info.Add(new Chunk(" 分钟", NORMAL));
  49:             return info;
  50:         }
  51:         public static Paragraph CreateMovieInformation(Movie movie)
  52:         {
     
  53:             Paragraph p = new Paragraph();
  54:             p.Font = (NORMAL);
  55:             p.Add(new Phrase("标题: ", BOLDITALIC));
  56:             p.Add(
  57:               movie.OriginalTitle);
  58:             p.Add(" ");
  59:             if (movie.OriginalTitle != null)
  60:             {
     
  61:                 p.Add(new Phrase(
  62:                   "正式标题: ", BOLDITALIC));
  63:                 p.Add(movie.OriginalTitle);
  64:                 p.Add(" ");
  65:             }
  66:             p.Add(new Phrase("国家: ", BOLDITALIC));
  67:             foreach (var country in movie.Countries)
  68:             {
     
  69:                 p.Add(
  70:                   CreateDirectorPhrase(country));
  71:                 p.Add(" ");
  72:             }
  73:             p.Add(new Phrase("导演: ", BOLDITALIC));
  74:             foreach (var director in movie.Directors)
  75:             {
     
  76:                 p.Add(
  77:                      CreateDirectorPhrase(director));
  78:                 p.Add(" ");
  79:             }
  80:             p.Add(CreateYearAndDuration(movie));
  81:             return p;
  82:         }

执行结果:

默认情况下,文本的对齐方式为左对齐,可以通过属性“Alignment”进行设置,

RIGHT. Element.ALIGN_JUSTIFIED_ALL 和Element.ALIGN_JUSTIFIED是很相似的,唯一的区别是前一种是占满整行,如下图

首行缩进(FirstLineIndent),左边缩进(tIndentationLeft),右边缩进(IndentationRight):

演示关键代码如下:

   1: var p = new Paragraph("这是一个缩进演示:段落是一系列块和(或)短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。说明:一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距。更改分割符 通常,当文本不能放在一行时,文本将被分割成不同的部分,iText首先会查找分割符,如果没有找到,文本将在行尾被截断。有一些预定的分割符如“ ”空格和“-”连字符,但是你可以使用setSplitCharacter方法来覆盖这些默认值。",NORMAL);
   2:             p.Alignment = Element.ALIGN_JUSTIFIED;
   3:             p.IndentationLeft = 12;
   4:             p.IndentationRight = 24;
   5:             p.FirstLineIndent = 24;
   6:             document.Add(p);
   7:  
   8:             p = new Paragraph("段落是一系列块和(或)短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。说明:一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距。更改分割符 通常,当文本不能放在一行时,文本将被分割成不同的部分,iText首先会查找分割符,如果没有找到,文本将在行尾被截断。有一些预定的分割符如“ ”空格和“-”连字符,但是你可以使用setSplitCharacter方法来覆盖这些默认值。", NORMAL);
   9:             p.Alignment = Element.ALIGN_JUSTIFIED;
  10:             document.Add(p);

段落间隔:

段落间隔使用如下两个属性:

SpacingAfter

SpacingBefore

进行设置。

文档在长度超过一行显示,使用分割符来进行断句换行,默认的分割符是空格和连字符,通过继承类“SplitCharacter”,来实现自定义的分割符。

四、列表(List)

列表就是一个有顺序的段落对象集合。

演示代码:

   1: static void Main(string[] args)
   2:         {
     
   3:             RegisterFont();
   4:             BOLD_UNDERLINED = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
   5:             NORMAL = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12);
   6:             BOLDITALIC = FontFactory.GetFont("华文宋体", BaseFont.IDENTITY_H, 12, Font.BOLD | Font.ITALIC);
   7:             Document document = new Document();
   8:             PdfWriter.GetInstance(document, new FileStream(string.Format("{0}helloworld.pdf",
   9:                 AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
  10:               .InitialLeading = 30;
  11:             document.Open();
  12:  
  13:             string[] contries = new string[] { "美国", "英甲", "中国", "朝鲜", "日本" };
  14:             document.Add(new Chunk("默认列表演示1:", NORMAL));
  15:             document.Add(Chunk.NEWLINE);
  16:           com.lowagie.text.List list = new com.lowagie.text.List();
  17:             for (int index = 1; index <= contries.Length; index++)
  18:             {
     
  19:                 var contry = contries[index - 1];
  20:                 list.Add(new ListItem(contry, NORMAL));
  21:             }
  22:             document.Add(list);
  23:             document.Add(Chunk.NEWLINE);
  24:             document.Add(new Chunk("不显示数字演示2:", NORMAL));
  25:             document.Add(Chunk.NEWLINE);
  26:  
  27:             list = new List(false);
  28:             for (int index = 1; index <= contries.Length; index++)
  29:             {
     
  30:                 var contry = contries[index - 1];
  31:                 list.Add(new ListItem(contry, NORMAL));
  32:             }
  33:             document.Add(list);
  34:             document.Add(Chunk.NEWLINE);
  35:  
  36:             document.Add(new Chunk("使用#作为列表符号3:", NORMAL));
  37:             document.Add(Chunk.NEWLINE);
  38:             list = new List();
  39:             list.SetListSymbol("#");
  40:             for (int index = 1; index <= contries.Length; index++)
  41:             {
     
  42:                 var contry = contries[index - 1];
  43:                 list.Add(new ListItem(contry, NORMAL));
  44:             }
  45:             document.Add(list);
  46:  
  47:             document.Add(Chunk.NEWLINE);
  48:             document.Add(new Chunk("显示数字演示4:", NORMAL));
  49:             document.Add(Chunk.NEWLINE);
  50:  
  51:             list = new List(true);
  52:             for (int index = 1; index <= contries.Length; index++)
  53:             {
     
  54:                 var contry = contries[index - 1];
  55:                 list.Add(new ListItem(contry, NORMAL));
  56:             }
  57:             document.Add(list);
  58:  
  59:             document.Add(Chunk.NEWLINE);
  60:             document.Add(new Chunk("RomanList演示5:", NORMAL));
  61:             document.Add(Chunk.NEWLINE);
  62:  
  63:             var list1 = new RomanList();
  64:             for (int index = 1; index <= contries.Length; index++)
  65:             {
     
  66:                 var contry = contries[index - 1];
  67:                 list1.Add(new ListItem(contry, NORMAL));
  68:             }
  69:             document.Add(list1);
  70:  
  71:             document.Add(Chunk.NEWLINE);
  72:             document.Add(new Chunk("GreekList演示6:", NORMAL));
  73:             document.Add(Chunk.NEWLINE);
  74:  
  75:             var list2 = new GreekList();
  76:             for (int index = 1; index <= contries.Length; index++)
  77:             {
     
  78:                 var contry = contries[index - 1];
  79:                 list2.Add(new ListItem(contry, NORMAL));
  80:             }
  81:             document.Add(list2);
  82:  
  83:             document.Add(Chunk.NEWLINE);
  84:             document.Add(new Chunk("ZapfDingbatsNumberList演示7:", NORMAL));
  85:             document.Add(Chunk.NEWLINE);
  86:  
  87:             var list3 = new ZapfDingbatsNumberList(10);
  88:             for (int index = 1; index <= contries.Length; index++)
  89:             {
     
  90:                 var contry = contries[index - 1];
  91:                 list3.Add(new ListItem(contry, NORMAL));
  92:             }
  93:             document.Add(list3);
  94:  
  95:             document.Add(Chunk.NEWLINE);
  96:             document.Add(new Chunk("ZapfDingbatsList演示8:", NORMAL));
  97:             document.Add(Chunk.NEWLINE);
  98:  
  99:             var list4 = new ZapfDingbatsList(10);
 100:             for (int index = 1; index <= contries.Length; index++)
 101:             {
     
 102:                 var contry = contries[index - 1];
 103:                 list4.Add(new ListItem(contry, NORMAL));
 104:             }
 105:             document.Add(list4);
 106:  
 107:             document.Close();
 108:         }

执行结果:

这个列表,还有一些扩展列表,如上面演示的5-7。

本文参考地址:http://www.cnblogs.com/LifelongLearning/archive/2011/03/30/2000072.html

其他好的参考地址:http://reymont.iteye.com/blog/1124217

  相关解决方案