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(); } }}