java iText chapter and section

Java iText chapter:

The Chapter is used to add the chapter in the pdf file. It is represented by com.itextpdf.text.Chapter class.

Java iText Section:

The Section is used to add the section in the pdf file. It is represented by com.itextpdf.text.Section class.


1. Create Document instance. It represents the current document to which we are adding content.
2. Create OutputStream instance. It represents the generated pdf.
3. Create PDFWriter instance and pass Document and OutputStream instance to its constructor.
4. Open the Document by calling
5. Create Chapter Object.
6. Add sections to the chapter object.
7. Add the content to the document by calling document.add() method using chapter object.
8. Close the document by calling document.close() method.


import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 * This class is used to create a pdf file using iText jar.
 * @author w3spoint
public class PDFChapterSectionExample {
  public static void main(String args[]){
    try {
	//Create Document instance.
	Document document = new Document();
	//Create OutputStream instance.
	OutputStream outputStream = 
	 new FileOutputStream(new File("D:\\TestChapterSectionFile.pdf"));
	//Create PDFWriter instance.
        PdfWriter.getInstance(document, outputStream);
        //Open the document.;
        //Create Chapter object
        Chapter chapter = new Chapter(new Paragraph("Chapter 1"), 1); 	
        //Add section to chapter
        chapter.addSection("Section 1", 2);
        chapter.addSection("Section 2", 2);
        //Add content to the document using chapter objects.
        //Close document and outputStream.
        System.out.println("Pdf created successfully.");
    } catch (Exception e) {


Pdf created successfully.

Download this example.
Next Topic: How to read an existing pdf file in java using iText jar?
Previous Topic: Java iText font.


Content Protection by
Please Share