`
liubl2011
  • 浏览: 234379 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

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

运行结果如下



小宝制造。
  • 大小: 50.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics