Source Code : Creating Chapter Section in PDF document

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Creating Chapter Section in PDF document

Creating Chapter Section in PDF document
import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;

public class ChapterSectionPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ChapterSectionPDF.pdf"));
      writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
      document.open();
      Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255, 0, 0));
      Font sectionFont = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, new Color(0, 0, 255));
      Font subsectionFont = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD, new Color(0, 64, 64));

      Paragraph paragraph1 = new Paragraph("text 1");
      Paragraph paragraph2 = new Paragraph("text 2");

      Paragraph cTitle = new Paragraph("This is a chapter ", chapterFont);
      Chapter chapter = new Chapter(cTitle, 1);

      paragraph2.setAlignment(Element.ALIGN_JUSTIFIED);
      paragraph1.setAlignment(Element.ALIGN_JUSTIFIED);
      chapter.add(paragraph1);
      
      Paragraph sTitle = new Paragraph("This is section 1 in chapter 1" , sectionFont);
      Section section = chapter.addSection(sTitle, 1);
      section.add(paragraph1);
      
      Paragraph subTitle = new Paragraph("This is subsection 1 of section 1", subsectionFont);
      Section subsection = section.addSection(subTitle, 3);
      
      document.add(chapter);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}
           
       

Thank with us