Java IText:章+节

时间:2020-01-09 10:36:08  来源:igfitidea点击:

我们可以使用IText将章节添加到PDF文档。章节由类" com.itextpdf.text.Chapter"表示。部分由类" com.itextpdf.text.Section"表示。

这是一个简单的代码示例:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

  }
}