上面两篇介绍了PDF文档的创建和中文支持设置方法,下面对文档经常使用的对象时行介绍:
块(Chunk)、短句(Phrase)、段落(Paragraph)、列表(List)
文档中的对象UML图,如下:
一、块(Chunk)
块(Chunk)是能被添加到文档的文本的最小单位,块可以用于构建其他基础元素如短句、段落、锚点等,块是一个有确定字体的字符串,要添加块到文档中时,其他所有布局变量均要被定义。
它有字体、大小、颜色、粗体,背景色、下划线,删除线等属性。
示例代码:
void RegisterFont()
2: {
);
);
5: FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
);
7: FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
);
9: FontFactory.Register(Environment.GetFolderPath(Environment.SpecialFolder.System) +
);
11: }
12:
string[] args)
14: {
15: RegisterFont();
new Document();
,
18: AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
19: .InitialLeading = 16;
20: document.Open();
21:
};
int index = 1; index <= contries.Length;index++ )
24: {
25: var contry = contries[index-1];
, BaseFont.IDENTITY_H, 16)));
));
new Font(
29: Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
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)是一系列以特定间距(两行之间的距离)作为参数的块,一个短句有一个主字体,但短句中的一些块具有不同于主字体的字体,你有更多的选择去创建短句。
简单来说,就是由多个同一行间距块组成。
演示代码,如下:
string[] args)
2: {
3: RegisterFont();
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
, BaseFont.IDENTITY_H, 12);
new Document();
,
8: AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
9: .InitialLeading = 30;
10: document.Open();
11:
};
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:
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
, BaseFont.IDENTITY_H, 12);
string str)
26: {
new Phrase();
new Chunk(str, BOLD_UNDERLINED));
, BOLD_UNDERLINED));
, NORMAL));
new Chunk(str, NORMAL));
return director;
33: }
执行结果:
默认情况下,行间距是1.5倍字体大小,可以不用设置行间距,就比较好的显示文档。
三、段落(Paragraph)
段落是一系列块和(或)短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。
说明:一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距。
更改分割符
通常,当文本不能放在一行时,文本将被分割成不同的部分,iText首先会查找分割符,如果没有找到,文本将在行尾被截断。有一些预定的分割符如“ ”空格和“-”连字符,但是你可以使用setSplitCharacter方法来覆盖这些默认值。
以使用IndentationLeft和IndentationRight,FirstLineIndent属性设置缩排;
演示代码如下:
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
, BaseFont.IDENTITY_H, 12);
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.ITALIC);
string str)
5: {
new Phrase();
//director.Add(new Chunk(str, BOLD_UNDERLINED));
new Chunk(str, NORMAL));
return director;
12: }
13:
string[] args)
15: {
16: RegisterFont();
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
, BaseFont.IDENTITY_H, 12);
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.ITALIC);
new Document();
,
22: AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
23: .InitialLeading = 30;
24: document.Open();
25:
};
} };
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:
static Paragraph CreateYearAndDuration(Movie movie)
39: {
new Paragraph();
41: info.Font = (NORMAL);
, BOLDITALIC));
new Chunk(movie.Year,
44: NORMAL));
, BOLDITALIC));
new Chunk(movie.Duration,
47: NORMAL));
, NORMAL));
return info;
50: }
static Paragraph CreateMovieInformation(Movie movie)
52: {
new Paragraph();
54: p.Font = (NORMAL);
, BOLDITALIC));
56: p.Add(
57: movie.OriginalTitle);
);
null)
60: {
new Phrase(
, BOLDITALIC));
63: p.Add(movie.OriginalTitle);
);
65: }
, BOLDITALIC));
in movie.Countries)
68: {
69: p.Add(
70: CreateDirectorPhrase(country));
);
72: }
, BOLDITALIC));
in movie.Directors)
75: {
76: p.Add(
77: CreateDirectorPhrase(director));
);
79: }
80: p.Add(CreateYearAndDuration(movie));
return p;
82: }
执行结果:
默认情况下,文本的对齐方式为左对齐,可以通过属性“Alignment”进行设置,
RIGHT. Element.ALIGN_JUSTIFIED_ALL 和Element.ALIGN_JUSTIFIED是很相似的,唯一的区别是前一种是占满整行,如下图
首行缩进(FirstLineIndent),左边缩进(tIndentationLeft),右边缩进(IndentationRight):
演示关键代码如下:
,NORMAL);
2: p.Alignment = Element.ALIGN_JUSTIFIED;
3: p.IndentationLeft = 12;
4: p.IndentationRight = 24;
5: p.FirstLineIndent = 24;
6: document.Add(p);
7:
, NORMAL);
9: p.Alignment = Element.ALIGN_JUSTIFIED;
10: document.Add(p);
段落间隔:
段落间隔使用如下两个属性:
SpacingAfter
SpacingBefore
进行设置。
文档在长度超过一行显示,使用分割符来进行断句换行,默认的分割符是空格和连字符,通过继承类“SplitCharacter”,来实现自定义的分割符。
四、列表(List)
列表就是一个有顺序的段落对象集合。
演示代码:
string[] args)
2: {
3: RegisterFont();
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.UNDERLINE);
, BaseFont.IDENTITY_H, 12);
, BaseFont.IDENTITY_H, 12, Font.BOLD | Font.ITALIC);
new Document();
,
9: AppDomain.CurrentDomain.BaseDirectory), FileMode.OpenOrCreate))
10: .InitialLeading = 30;
11: document.Open();
12:
};
, NORMAL));
15: document.Add(Chunk.NEWLINE);
new List();
int index = 1; index <= contries.Length; index++)
18: {
19: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
21: }
22: document.Add(list);
23: document.Add(Chunk.NEWLINE);
, NORMAL));
25: document.Add(Chunk.NEWLINE);
26:
false);
int index = 1; index <= contries.Length; index++)
29: {
30: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
32: }
33: document.Add(list);
34: document.Add(Chunk.NEWLINE);
35:
, NORMAL));
37: document.Add(Chunk.NEWLINE);
new List();
);
int index = 1; index <= contries.Length; index++)
41: {
42: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
44: }
45: document.Add(list);
46:
47: document.Add(Chunk.NEWLINE);
, NORMAL));
49: document.Add(Chunk.NEWLINE);
50:
true);
int index = 1; index <= contries.Length; index++)
53: {
54: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
56: }
57: document.Add(list);
58:
59: document.Add(Chunk.NEWLINE);
, NORMAL));
61: document.Add(Chunk.NEWLINE);
62:
new RomanList();
int index = 1; index <= contries.Length; index++)
65: {
66: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
68: }
69: document.Add(list1);
70:
71: document.Add(Chunk.NEWLINE);
, NORMAL));
73: document.Add(Chunk.NEWLINE);
74:
new GreekList();
int index = 1; index <= contries.Length; index++)
77: {
78: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
80: }
81: document.Add(list2);
82:
83: document.Add(Chunk.NEWLINE);
, NORMAL));
85: document.Add(Chunk.NEWLINE);
86:
new ZapfDingbatsNumberList(10);
int index = 1; index <= contries.Length; index++)
89: {
90: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
92: }
93: document.Add(list3);
94:
95: document.Add(Chunk.NEWLINE);
, NORMAL));
97: document.Add(Chunk.NEWLINE);
98:
new ZapfDingbatsList(10);
int index = 1; index <= contries.Length; index++)
101: {
102: var contry = contries[index - 1];
new ListItem(contry, NORMAL));
104: }
105: document.Add(list4);
106:
107: document.Close();
108: }
执行结果:
这个列表,还有一些扩展列表,如上面演示的5-7。