Create chapter PDF using iText

Create chapter PDF using iText

To create new chapter in PDF using iText you will have to instantiate Chapter class and add paragraph and section to it.

Important: To create pdf iTextxxx.jar should be available in your class path you can download in bottom download link or it can be also downloaded from its web site here

Please have below sample java code for this:

package com.javahonk.createpdf;

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

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfWriter;

public class CreateChapterPDF {

    private static Font TIME_ROMAN = 
            new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);

    /**
     * @param args
     */
    public static void main(String[] args) {
    Document document = new Document();
    try {
        PdfWriter.getInstance(document,
                new FileOutputStream("C:/PDF/CreateChapter.pdf"));          
        document.open();            
        addChapter(document);               
        document.close();
    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
    }       

    }   

    private static void addChapter(Document document) 
            throws DocumentException {

    Anchor anchor = new Anchor("First Chapter", TIME_ROMAN);
    anchor.setName("First Chapter");
    Chapter chapter = new Chapter(new Paragraph(anchor), 1);        
    Paragraph paragraph = new Paragraph("Sub-Heading", TIME_ROMAN);
    Section section = chapter.addSection(paragraph);

    paragraph = new Paragraph("Paragraph in center");
    paragraph.setAlignment(Element.ALIGN_CENTER);
    section.add(paragraph);     

    section.add(new Paragraph("Paragraph no alignment"));
    section.add(new Paragraph(" "));        
    List list = new List(false, true, 15);
    list.add(new ListItem("List points lettered"));
    list.add(new ListItem("List points lettered"));
    list.add(new ListItem("List points lettered"));
    section.add(list);      

    list = new List(true, false, 15);
    list.add(new ListItem("List points numbered"));
    list.add(new ListItem("List points numbered"));
    list.add(new ListItem("List points numbered"));
    section.add(new Paragraph(" "));
    section.add(list);
    section.add(new Paragraph(" "));
    paragraph = new Paragraph("Sub-Heading", TIME_ROMAN);
    section = chapter.addSection(paragraph);        
    section.add(new Paragraph("Paragraph not align"));
    section.add(new Paragraph(" "));        
    list = new List(true, false, 15);
    list.add(new ListItem("List points numbered"));
    list.add(new ListItem("List points numbered"));
    list.add(new ListItem("List points numbered"));

    section.add(list);

    document.add(chapter);      

    }

}

 

Output:

Create chapter PDF using iText

 

Dynamically generate PDF using servlet Download iTextJar

Leave a Reply

Your email address will not be published. Required fields are marked *