Create PDF java using iText

Create PDF java using iText

iText provides API that is easy to use and create PDF in java. Below is sample java code which will show you how to create PDF using iText.

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

Below are steps to create table in PDF:

  • Create document
  • Call PdfWriter.getInstance method and pass document object, OutputStream(File name)
  • Open document for writing
  • Start writing content to it.

Sample code:

package com.javahonk.createpdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

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.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {

    private static Font TIME_ROMAN_BIG = 
            new Font(Font.FontFamily.TIMES_ROMAN, 20,Font.BOLD);
    private static Font TIME_ROMAN_SMALL = 
            new Font(Font.FontFamily.TIMES_ROMAN, 13,Font.BOLD);

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            Document document = new Document();
            PdfWriter.getInstance(document, 
                    new FileOutputStream("C:/PDF/GeneratePDF.pdf"));
            document.open();
            addMetaDataToPDF(document);
            addTitlePageToPDF(document);            
            createTableOnPDF(document);
            addChapterToPDF(document);
            document.close();

        } catch (FileNotFoundException | DocumentException e) {         
            e.printStackTrace();
        }

    }

    private static void addMetaDataToPDF(Document document) {
        document.addTitle("Create PDF table");
        document.addSubject("Create PDF table");
        document.addAuthor("Java Honk");
        document.addCreator("Java Honk");
    }

    private static void addTitlePageToPDF(Document document)
            throws DocumentException {

        Paragraph paragraph = new Paragraph();
        paragraph.add(new Paragraph(" "));
        paragraph.add(new Paragraph("Create PDF in Java using iText", 
                TIME_ROMAN_BIG));

        paragraph.add(new Paragraph(" "));
        SimpleDateFormat simpleDateFormat = 
                new SimpleDateFormat("MM/dd/yyyy");
        paragraph.add(new Paragraph("PDF created date "+ 
                simpleDateFormat.format(new Date()),TIME_ROMAN_SMALL));
        document.add(paragraph);

    }

    private static void createTableOnPDF(Document document) 
            throws DocumentException {      

        Paragraph paragraph = new Paragraph();
        paragraph.add(new Paragraph(" "));
        paragraph.add(new Paragraph(" "));
        document.add(paragraph);
        PdfPTable table = new PdfPTable(3);     

        PdfPCell c1 = new PdfPCell(new Phrase("First Name"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Last Name"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Test"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.setHeaderRows(1);

        for (int i = 0; i < 5; i++) {
            table.getDefaultCell().setHorizontalAlignment(
                    Element.ALIGN_CENTER);
            table.getDefaultCell().setVerticalAlignment(
                    Element.ALIGN_MIDDLE);
            table.addCell("Java");
            table.addCell("Honk");
            table.addCell("Success");
        }       

        document.add(table);
    }

    private static void addChapterToPDF(Document document) 
            throws DocumentException {

    Anchor anchor = new Anchor("First Chapter", TIME_ROMAN_BIG);
    anchor.setName("First Chapter");
    Chapter chapter = new Chapter(new Paragraph(anchor), 1);        
    Paragraph paragraph = new Paragraph("Sub-Heading", 
            TIME_ROMAN_BIG);
    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_BIG);
    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 page 1:

Create PDF java using iText
Output page 2:
Create PDF java using iText

 

Dynamically generate PDF using servlet Download iTextJar

Leave a Reply

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

I am not Robot *