Friday 21 October 2016

itext: Chapter and Section

What is Section?
Section is part of the document, where you can add paragraphs, list of elements, tables etc., Section class provides following constructors to define Section instance.

protected Section()
protected Section(final Paragraph title, final int numberDepth)

As you see, the constructors are protected. So you can use Section class by using the subclasses of Section class.

What is Chapter?
Chapter extends the functionality of Section class.

public class Chapter extends Section {
         ....
}

Chapter class provide following constructors to work with it.

public Chapter(int chapterNumber)
public Chapter(Paragraph title, int chapterNumber)
public Chapter(String title, int chapterNumber)

The chapter number is shown by default. If you don't want to see the chapter number, you have to set the numberdepth to 0.

public void setNumberDepth(final int numberDepth)
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.pdf.PdfWriter;

public class TestChapter {
 public static void main(String args[]) throws FileNotFoundException, DocumentException {
  Paragraph title = new Paragraph("Introduction To Java");
  Chapter chapter1 = new Chapter(title, 1);

  chapter1.addSection("Features of Java");
  chapter1.addSection("Hello World to Java");
  chapter1.addSection("Primitive types");

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream("tutorials.pdf"));
  document.open();
  document.add(chapter1);
  document.close();
 }
}

Run above application, you can able to see following kind of screen.


Previous                                                 Next                                                 Home

No comments:

Post a Comment