Itext 学习笔记(五) Chapters (章节)的用法

Itext的com.itextpdf.text.Chapter类设置章,com.itextpdf.text.Section类设置节。

例子代码如下

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]

小宝制造。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值