Home  >  iText

Create PDF with Text, List and Table in Java Using iText

By Arvind Rai, February 05, 2015
In this page we will create PDF in java using iText API. iText is an open source that provides API for PDF. We can easily create and manipulate PDF files. In web development, dynamically PDF creation and manipulation can be done using iText. Here in this page, we will add text in PDF, create a table and list. We are also providing some basic iText API description. We are providing examples to add text, create table and list in PDF using iText API.

Gradle To Resolve iText JAR

Find the gradle file to resolve iText JAR.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'ITEXT'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.itextpdf:itextpdf:5.5.4'
} 

iText API Description

Find the description of commonly used iText API.
com.itextpdf.text.Document: Represents the generic document in which elements like paragraph, list tables etc are added and then finally this document is written to the PDF file.
com.itextpdf.text.Paragraph: Create a paragraph with some data and then added to document to write in PDF.
com.itextpdf.text.pdf.PdfWriter: Takes document and PDF file name as an argument and then writes document in PDF file.
com.itextpdf.text.Rectangle: Helps to create custom size PDF file.
com.itextpdf.text.BaseColor: Helps to define color in PDF file like background color, text color etc.
com.itextpdf.text.DocumentException: If there is an exception in Document, DocumentException is thrown.
com.itextpdf.text.Element: Interface containing many constant like to align text in PDF.
com.itextpdf.text.pdf.PdfPCell: Represents a table cell in PDF.
com.itextpdf.text.pdf.PdfPTable: Represents a table in PDF.

Add Text to PDF

We are adding a text in PDF. This is the simplest example to create a PDF.
AddText.java
package com.concretepage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class AddText {
   public static void main(String[] args) throws FileNotFoundException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("sample1.pdf"));
        document.open();
        document.add(new Paragraph("Sample 1: Hello, this is Concretepage.com"));
        document.close();
        System.out.println("Done");
   }
} 
Find the output.
Create PDF with Text, List and Table in Java Using iText

Create Custom PDF Page Size

Here we are creating a PDF page with custom size.
CustomPDFPageSize.java
package com.concretepage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
public class CustomPDFPageSize {
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
	Rectangle pagesize = new Rectangle(297,420);
        Document document = new Document(pagesize, 36, 76, 36, 76);
        PdfWriter.getInstance(document, new FileOutputStream("sample2.pdf"));
        document.open();
        document.add(new Paragraph("Sample 2: Custom Page Size Demo."));
        document.close();
        System.out.println("Done");
    }
} 

Create Table in PDF

In this example, we are creating a table in PDF
CreateTable.java
package com.concretepage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class CreateTable {
       public static void main(String[] args) throws FileNotFoundException, DocumentException {
	  Document document = new Document();
	  PdfPTable table = new PdfPTable(new float[] { 2, 1, 2 });
	  table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	  table.addCell("Name");
          table.addCell("Age");
          table.addCell("Location");
	  table.setHeaderRows(1);
	  PdfPCell[] cells = table.getRow(0).getCells(); 
	  for (int j=0;j<cells.length;j++){
	     cells[j].setBackgroundColor(BaseColor.GRAY);
	  }
          for (int i=1;i<5;i++){
    	     table.addCell("Name:"+i);
             table.addCell("Age:"+i);
             table.addCell("Location:"+i);
          }
	  PdfWriter.getInstance(document, new FileOutputStream("sample3.pdf"));
	  document.open();
          document.add(table);
	  document.close();
	  System.out.println("Done");
      }
} 
Find the output.
Create PDF with Text, List and Table in Java Using iText

Create List in PDF

Now we are creating list in PDF.
CreateList.java
package com.concretepage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;
public class CreateList {
   public static void main(String[] args) throws FileNotFoundException, DocumentException {
	Document document = new Document();
	List list = new List(List.ORDERED);
        list.setAutoindent(false);
        list.setSymbolIndent(42);	
        list.add(new ListItem("Ram"));	
        list.add(new ListItem("Shyam"));
        list.add(new ListItem("Mohan"));
        list.add(new ListItem("Ganesh"));
        PdfWriter.getInstance(document, new FileOutputStream("sample4.pdf"));
        document.open();
        document.add(list);
	document.close();
	System.out.println("Done");
   }
} 
Find the output.
Create PDF with Text, List and Table in Java Using iText
Now we are done. Happy Learning.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us