Creating a PDF file in Java

jPDFCreator.java

	import com.itextpdf.text.BaseColor;
	import com.itextpdf.text.Chapter;
	import com.itextpdf.text.Document;
	import com.itextpdf.text.DocumentException;
	import com.itextpdf.text.Font;
	import com.itextpdf.text.FontFactory;
	import com.itextpdf.text.List;
	import com.itextpdf.text.ListItem;
	import com.itextpdf.text.PageSize;
	import com.itextpdf.text.Paragraph;
	import com.itextpdf.text.Section;
	import com.itextpdf.text.pdf.PdfWriter;
	import java.io.FileNotFoundException;
	import java.io.FileOutputStream;


	/**
	 *
	 * @author dhanoopbhaskar
	 */
	public class jPDFCreator {


	    Document document = null;
	    PdfWriter pdfWriter = null;
	    
	    public jPDFCreator() {
	        /**
	         * Creating an instance of com.itextpdf.text.Document
	         * The arguments
	         * -page size
	         * -left margin
	         * -right margin
	         * -top margin
	         * -bottom margin
	         */
	        document = new Document(PageSize.A4, 50, 50, 50, 50);
	        try {
	            /**
	             * Arguments
	             * -instance of Document
	             * -OutputStream of file to which the data has to be written
	             */
	            pdfWriter = PdfWriter.getInstance(
	                    document, new FileOutputStream("jPDF.pdf"));            
	        } catch (FileNotFoundException ex) {
	            System.out.println(ex);
	        } catch (DocumentException ex) {
	            System.out.println(ex);
	        }
	        /**
	         * Open the document
	         */
	        document.open();
	        try {
	            /**
	             * Adding a paragraph to the document
	             */
	            document.add(new Paragraph("First Paragraph."));
	            /**
	             * Creating an instance of com.itextpdf.text.Font
	             * Arguments
	             * -font name
	             * -size
	             * -style
	             * -base color
	             */
	            Font font = FontFactory.getFont(FontFactory.COURIER, 14, 
	                    Font.BOLD, BaseColor.BLUE);
	            /**
	             * Pass an instance of font in new Paragraph() to set font.
	             */
	            document.add(new Paragraph("Second Paragraph in a different "
	                    + "color and font type.", font));
	            /**
	             * Creating a Chapter
	             * Arguments
	             * -title
	             * -number
	             * Setting number depth to 0 - to hide chapter number on page.
	             */
	            Paragraph chapterTitle = new Paragraph("Chapter 1", font);
	            Chapter chapter1 = new Chapter(chapterTitle, 1);
	            chapter1.setNumberDepth(0);
	            /**
	             * Creating Sections under the Chapter
	             */
	            Paragraph sectionTitle = new Paragraph(
	                    "Section 1 in Chapter 1", font);
	            Section section1 = chapter1.addSection(sectionTitle);
	            Paragraph textUnderSection = new Paragraph("Some text "
	                    + "under the section 1 in the chapter 1");
	            section1.add(textUnderSection);
	            section1.setNumberDepth(0);            
	            Paragraph someText = new Paragraph("Following is a list.");
	            section1.add(someText);
	            /**
	             * Creating a list
	             * Arguments
	             * -numbered
	             * -lettered
	             * -symbolIndent
	             */
	            List list = new List(true, false, 10);
	            /**
	             * Adding list items
	             */
	            list.add(new ListItem("First item in the list"));
	            list.add(new ListItem("Second item in the list"));
	            list.add(new ListItem("Third item in the list"));
	            /**
	             * Adding the list to section1
	             */
	            section1.add(list);
	            /**
	             * Adding chapter1 to document
	             */
	            document.add(chapter1);            
	            document.close();
	        } catch (DocumentException ex) {
	            System.out.println(ex);
	        }
	        
	    }       
	    
	    public static void main(String[] args) {
	        new jPDFCreator();
	    }
	}

SCREENSHOTS

Page1

Page2

Program with the statements:

chapter1.setNumberDepth(0);

and 

section1.setNumberDepth(0);

Page2

Program without the statements:

chapter1.setNumberDepth(0);

and

section1.setNumberDepth(0);