Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 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"));;

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

Previous                                                 Next                                                 Home

This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

itext: Chapter and Section


Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription