上一篇对块,语句、段落、列表进行了演示,本篇将对更高级的对象Anchor、Image、Chapter、Section的使用方法进行演示。
一、Anchor
Anchor对象可以在文档内部或向外部跳转。内部跳转常见的应用就是目录,外部跳转就是跳转到指定的文件或网页。下面代码演示文档内部跳转和跳转到博客园点。
class AnchorDemo : TestBase
2: {
void WriteDocument(Document document, PdfWriter writer)
4: {
new Paragraph();
);
7:
,Normal);
;
, Normal);
;
, Normal);
;
14: p.Add(anchor);
15: p.Add(anchor2);
16: p.Add(anchor3);
17:
, Normal);
;
20: document.Add(p);
21: document.Add(anchor10);
22:
23: document.NewPage();
, Normal);
;
26: document.Add(anchor1);
27:
, Normal);
;
30: document.Add(anchor4);
31:
32: document.NewPage();
, Normal);
;
35: document.Add(anchor5);
36:
, Normal);
;
39: document.Add(anchor6);
40: }
41: }
本节对演示代码进行了重构,基类代码请查看:一步一步 IText.Sharp
上面的代码演示结果如下:
Chunk也可以创建跳转链接,
Chunk.SetLocalDestination 设置本地描述名称,可以说它是当前锚点的名称
Chunk.SetLocalGoto 设置本地跳转到锚点
Chunk.SetRemoteGoto 设置远程跳转,如url
示例代码如下:
class GoTopDemo : TestBase
2: {
void WriteDocument(Document document, PdfWriter writer)
4: {
new Paragraph();
, Bolditalic);
);
8: p.Add(top);
9: document.Add(p);
10: Chunk imdb =
, Bolditalic);
));
,Normal);
14: p.Add(imdb);
);
16: document.Add(p);
);
, Normal);
, 1);
20: p.Add(page1);
);
22: document.Add(p);
23:
24: document.NewPage();
, Normal);
, Normal);
);
28: p.Add(top);
);
30: document.Add(p);
31: document.NewPage();
, Normal);
, Normal);
);
35: p.Add(top);
);
37: document.Add(p);
38:
39: document.NewPage();
, Normal);
, Normal);
);
43: p.Add(top);
);
45: document.Add(p);
46:
47: document.NewPage();
);
);
);
51: p.Add(top);
);
53: document.Add(p);
54:
55: document.NewPage();
);
);
);
59: p.Add(top);
);
61: document.Add(p);
62: }
63: }
二、Chapter与Section
Chapter和Section用来创建书签和文档结构非常有用。同时使用PdfOutline也可以创建书签,默认情况下,生成章节和分区使用的是数字标识,如下所示:
代码如下:
class ChapterDemo2 : TestBase
2: {
void WriteDocument(Document document, PdfWriter writer)
4: {
};
6:
null;
null;
null;
null;
, Normal);
new Chapter(title, 1);
, Bolditalic);
14: section = chapter.AddSection(title, 2);
, Normal);
16: subsection = section.AddSection(title);
17:
18: subsection.IndentationLeft = (20);
19: subsection.NumberDepth = (1);
+ 12, Normal));
, Normal));
22: subsection.Add(GetDirectorList(contries));
, Normal));
24: subsection.Add(GetDirectorList(contries));
25:
26: document.Add(chapter);
27: }
28:
string[] list)
30: {
new List();
in list)
33: {
new ListItem(c, Normal));
35: }
36:
37:
return result;
39: }
40: }
三、Image
在ITextSharp中使用Image.getInstance方法创建Image对象,支持的文件类型包括:
Jpeg, PngImage, GifImage,
TiffImage
itextsharp会自动调整图文的内容,以让页面显示更多的内容,如果要禁用这种情况,就要使用如下方法:
true;
强制使用添加顺序。
图像有对象方式,有如下几种:左对齐、右对齐、居中对齐、自动调整,
图像的位置可以通过,上下左右来设置控制,同时可以通过:ScalePercent,ScaleToFit,
ScaleAbsolute,scaleAbsoluteWidth,scaleAbsoluteHeight调整图像显示大小,对于布局的控制非常方便。
备注:如果同一个图像创建多次并都添加到文档中,文档的大小就增长很快,如果一个图像要在多个地方使用,只要生成一个Image对象,在添加到文档前,设置其属性就可以了,没有必要创建多份。
图像还支持旋转:
RotationDegrees,这个是设置旋转的角度,可以设置正负数。
演示示例效果如下:
代码如下:
class ImageDemo : TestBase
2: {
void WriteDocument(Document document, PdfWriter writer)
4: {
true;
, Normal));
7: Image image = CreateImageObject();
8: image.Alignment = Image.ALIGN_LEFT;
9: image.ScalePercent(30);
10: document.Add(image);
11:
, Normal));
//image = CreateImageObject();
14: image.ScalePercent(30);
15: image.Alignment = Image.ALIGN_RIGHT;
16: document.Add(image);
17:
, Normal));
//image = CreateImageObject();
20: image.Alignment = Image.ALIGN_MIDDLE;
21: image.ScaleToFit(150,300);
22: document.Add(image);
23:
, Normal));
//image = CreateImageObject();
26: image.Alignment = Image.ALIGN_JUSTIFIED_ALL;
27: image.ScalePercent(30);
28: document.Add(image);
29: document.NewPage();
, Normal);
31: p.Add(Chunk.NEWLINE);
32:
33: p.Add(Chunk.NEWLINE);
34: p.Add(Chunk.NEWLINE);
35: p.Add(Chunk.NEWLINE);
36: p.Add(Chunk.NEWLINE);
37: p.Add(Chunk.NEWLINE);
38: p.Add(Chunk.NEWLINE);
39: p.Add(Chunk.NEWLINE);
40: p.Add(Chunk.NEWLINE);
41: p.Add(Chunk.NEWLINE);
42: p.Add(Chunk.NEWLINE);
43: p.Add(Chunk.NEWLINE);
44: p.Add(Chunk.NEWLINE);
45: image.Alignment = Image.ALIGN_JUSTIFIED_ALL;
46: image.ScalePercent(30);
47: image.RotationDegrees = 30;
new Chunk(image, 0, -30));
49: document.Add(p);
50:
, Normal);
52: p.Add(Chunk.NEWLINE);
53: p.Add(Chunk.NEWLINE);
54: p.Add(Chunk.NEWLINE);
55: p.Add(Chunk.NEWLINE);
56: p.Add(Chunk.NEWLINE);
57: p.Add(Chunk.NEWLINE);
58: p.Add(Chunk.NEWLINE);
59: p.Add(Chunk.NEWLINE);
60: p.Add(Chunk.NEWLINE);
61: p.Add(Chunk.NEWLINE);
62: p.Add(Chunk.NEWLINE);
63: p.Add(Chunk.NEWLINE);
64: p.Add(Chunk.NEWLINE);
65: image.Alignment = Image.ALIGN_JUSTIFIED_ALL;
66: image.ScalePercent(30);
67: image.RotationDegrees = -30;
new Chunk(image, 0, -15));
69: document.Add(p);
70: }
71:
private Image CreateImageObject()
73: {
);
75: }
76: }