document.open();
/** *//**
* 现在,我们往document的第一页中加入一些文本内容。
* 所有的文本都要使用com.lowagie.text.Paragraph才能添加。
* 可以创建一个默认的paragraph,使用默认的字体,颜色,尺寸等属性,也可使使用自己定义的字体。
* 下面分别进行了这两种操作。
*/
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph(
"Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14,
Font.BOLD, new Color(255, 150, 200))));
/** *//**
* 接下来,我们往document中添加一些复杂的元素。
* 让我们从创建一个新的chapter开始。
* chapter是一个特殊的部分,它将从新的一页开始,默认显示数字序号。
*/
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(
FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0,
255)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
/** *//**
* Section是chapter的一个子元素。
* 在下面的代码中我们创建了一个题为"This is Section 1 in Chapter 1" 的Section。
* 为了在Section下添加文本,我们再创建一个Paragraph对象,将其增加到Section对象中。
*/
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph(
"This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
/** *//**
* 表格中含有一个行和列的矩阵。
* 一行中的单元格(cell)可以延伸到多列。
* 同样的,一列中的单元格可以延伸到多行。
* 因此,一个3x2的表格不需要6个单元格。
*/
Table t = new Table(3, 2);
//设置边框颜色。如果颜色和背景色一样,则边框不会显示出来
t.setBorderColor(Color.white);
//设置单元格内文本的间距
t.setPadding(5);
//设置相邻单元格的间距
t.setSpacing(5);
t.setBorderWidth(1);
/** *//**
* 然后,创建3个单元格对象,包含不同的内容。
* 依次将其放入表格中。
* 先放在第一行第一列,再放在同一行的下一列中,……
* 当一行填满之后,下一个单元格将放在下一行的第一列中。
* 只给定单元格的内容(不新建单元格)也可以新增一个单元格,比如t.addCell("1.1");。
* 最后,将表格添加到Section对象中。
*/
Cell c1 = new Cell("Header1");
//设置单元格边框颜色,设置方法和表格边框一样
c1.setBorderColor(Color.WHITE);
t.addCell(c1);
c1 = new Cell("Header2");
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
/** *//**
* 接下来,往PDF文档中添加一个列表(List)。
* 一个列表包含许多列表项(ListItem)。
* 列表可以编号也可以不编号。
* 将List的第一个参数置true表示