How do I use iText Paragraph class?

The com.itextpdf.text.Paragraph class represent a paragraph in PDF document. The following example show you how to create a simple paragraph. First we create a Paragraph object and then add some texts into it using the Chunk object.

package org.kodejava.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

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

public class ParagraphDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            FileOutputStream fos = new FileOutputStream("ParagraphDemo.pdf");
            PdfWriter.getInstance(doc, fos);
            doc.open();

            String content = "The quick brown fox jumps over the lazy dog";
            Paragraph paragraph = new Paragraph();
            for (int i = 0; i < 20; i++) {
                Chunk chunk = new Chunk(content);
                paragraph.add(chunk);
            }

            doc.add(paragraph);
            doc.close();
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I customize the Phrase object in iText?

This example shows how to customize the iText Phrase object. We can change the default line spacing of the Phrase object by passing a float parameter. We can also use the setLeading() method.

We can also set the default font for the Phrase so that every Chunk added to this object will have the same font. If you want the chunk to have a different font, you can set it in the chunk object itself.

Let’s see an example below:

package org.kodejava.itextpdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

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

public class PhraseCustomizeDemo {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("PhraseCustomizeDemo.pdf"));
            document.open();

            // Creates a phrase object, sets leading to 32 and
            // adds some chunks.
            Phrase phrase1 = new Phrase(20);

            // Set main font information of the Phrase object.
            phrase1.setFont(FontFactory.getFont(FontFactory.COURIER, 12,
                    Font.BOLD, new BaseColor(0, 0, 255)));

            for (int i = 0; i < 50; i++) {
                phrase1.add(new Chunk("Hello "));
            }

            // Add chunk to the phrase and replace the font information
            // for this chunk.
            phrase1.add(new Chunk("Hello",
                    FontFactory.getFont(FontFactory.HELVETICA)));
            document.add(phrase1);

            // Creates a phrase by defining the leading and a string.
            Phrase phrase2 = new Phrase(40, "Hello World!!!");
            document.add(phrase2);

            // Creates a phrase by defining the leading and add a chunk
            // into the phrase. The chunk has it own font face, font
            // style and color.
            Phrase phrase3 = new Phrase(50,
                    new Chunk("Hello I am BIG", FontFactory.getFont(
                            FontFactory.HELVETICA, 40,
                            Font.ITALIC, new BaseColor(255, 0, 0))));
            document.add(phrase3);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I use iText Phrase class?

The com.itextpdf.text.Phrase represent a phrase of text in the Document object. The Phrase object knows how to add spacing between lines of text. So if we add some phrase into the document it will start on a new line when it reaches the right edge of the document. The default leading / line spacing of a Phrase object is 16.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfWriter;

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

public class PhraseDemo {
    public static void main(String[] args) {
        // Creates an instance of Document.
        Document document = new Document();
        try {
            // Write the pdf document into a file using the PdfWriter
            // and passes the document object and a FileOutputStream.
            PdfWriter.getInstance(document, new FileOutputStream("PhraseDemo.pdf"));
            document.open();

            // Add a some Phrase element into the document.
            document.add(new Phrase("This is the first text "));
            document.add(new Phrase("This is the second text "));
            document.add(new Phrase("This is the third text "));
            document.add(new Phrase("This is the fourth text "));
            document.add(new Phrase("This is the fifth text "));
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I use iText Chunk class?

A chunk com.itextpdf.text.Chunk is the smallest significant part that can be added to a document. A chunk is a string with Font information.

A chunk doesn’t add line breaks, a paragraph spacing or any other spacing. The layout parameters of a chunk should be defined in the object to which this chunk is added.

When you run this example the chunk will be written from left to right. But when it reaches the right edge of the document it will start writing another chunk from the left overwriting the previous chunk.

package org.kodejava.itextpdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

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

public class ChunkDemo {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("ChunkDemo.pdf"));
            document.open();

            // Add some chunks into the document object.
            document.add(new Chunk("The quick brown fox "));
            document.add(new Chunk("jumps over the lazy dog."));
            document.add(new Chunk("The quick brown fox "));
            document.add(new Chunk("jumps over the lazy dog."));
            document.add(new Chunk("The quick brown fox ",
                    FontFactory.getFont(FontFactory.HELVETICA, 20,
                            Font.ITALIC, new BaseColor(0, 128, 0))));
            document.add(new Chunk("jumps over the lazy dog."));
            document.close();
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I use iText Document class?

The com.itextpdf.text.Document is the core class of the iText library. This class represents a pdf document. Basically to work with a document we must first create an instance of a Document. After the instance created we open the document by calling the open() method. To add some content into the document we can use the add() method. Finally, after done with the document we must close it by calling the close() method.

If you need to write and flush the document into a file we can use a PdfWriter class. The class constructor accept a Document object and an OutputStream object to do the work.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;

public class DocumentDemo {
    public static void main(String[] args) {
        // Creates an instance of a Document.
        Document document = new Document();
        try {
            // To work with a Document we must open the document, add
            // some contents and finally close the document.
            document.open();
            document.add(new Paragraph("Hello World!"));
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central