首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

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

2012-07-05 
Itext 学习笔记(五) Chapters(章节)的用法Itext的com.itextpdf.text.Chapter类设置章,com.itextpdf.text.S

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跟章得头是一个级别了。

运行结果如下



小宝制造。

热点排行