Itext的com.itextpdf.text.Chapter类设置章,com.itextpdf.text.Section类设置节。
例子代码如下
代码中chapter.addSection("This is section 1", 2)中的2是设置深度,如果设置成1跟章得头是一个级别了。
运行结果如下
[img]http://dl.iteye.com/upload/attachment/0062/6024/94c44eb6-2b72-353b-9215-b4582c7eea42.png[/img]
小宝制造。
例子代码如下
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfWriter;
public class ChapterSectionExample {
public static void main(String[] args) {
//定义文本
Document document = new Document();
try {
//文档写入
PdfWriter.getInstance(document, new FileOutputStream("ChapterSection.pdf"));
//文档打开
document.open();
//定义段落
Paragraph paragraph = new Paragraph();
//添加段落内容
paragraph.add(new Phrase("This is a chapter."));
//定义章
Chapter chapter = new Chapter(paragraph, 1);
//添加章节内容
Section section1 = chapter.addSection("This is section 1", 2);
Section section2 = chapter.addSection("This is section 2", 2);
//添加章节
document.add(chapter);
//关闭文档
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
代码中chapter.addSection("This is section 1", 2)中的2是设置深度,如果设置成1跟章得头是一个级别了。
运行结果如下
[img]http://dl.iteye.com/upload/attachment/0062/6024/94c44eb6-2b72-353b-9215-b4582c7eea42.png[/img]
小宝制造。