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

How do I create a table in PDF document in iText?

This example shows how to create a table in a PDF document. Using the iText PDF library we can use the PdfPTable and the PdfPCell classes to create table and cells in our PDF document. In the following example we use array of strings to define the table’s data.

Let’s see the complete code snippet below:

package org.kodejava.itextpdf;

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

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

public class TableDemo {
    public static void main(String[] args) {
        String[] headers = new String[]{"No", "Username", "First Name", "Last Name"};
        String[][] rows = new String[][]{
                {"1", "jdow", "John", "Dow"},
                {"2", "stiger", "Scott", "Tiger"},
                {"3", "fbar", "Foo", "Bar"}
        };

        // Create a new document.
        Document document = new Document(PageSize.LETTER.rotate());

        try {
            // Get an instance of PdfWriter and create a Table.pdf file
            // as an output.
            PdfWriter.getInstance(document, new FileOutputStream("TableDemo.pdf"));
            document.open();

            // Create an instance of PdfPTable. After that we transform
            // the header and rows array into a PdfPCell object. When
            // each table row is complete we have to call the
            // table.completeRow() method.
            //
            // For better presentation we also set the cell font name,
            // size and weight. And we also define the background fill
            // for the cell.
            Font fontHeader = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            Font fontRow = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);

            PdfPTable table = new PdfPTable(headers.length);
            for (String header : headers) {
                PdfPCell cell = new PdfPCell();
                cell.setGrayFill(0.9f);
                cell.setPhrase(new Phrase(header.toUpperCase(), fontHeader));
                table.addCell(cell);
            }
            table.completeRow();

            for (String[] row : rows) {
                for (String data : row) {
                    Phrase phrase = new Phrase(data, fontRow);
                    table.addCell(new PdfPCell(phrase));
                }
                table.completeRow();
            }

            document.addTitle("PDF Table Demo");
            document.add(table);
        } 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 create a PDF document using iText PDF?

This example is the first example of the iText PDF series, we are going to start by learning to use the iText PDF library to create a PDF document. We will see how to use the Document class, getting an instance of PdfWriter, creating a simple Paragraph and set the text alignment and the font.

The basic steps to produce a PDF documents are:

  • Create a Document object that represents a PDF document.
  • The PdfWrite object that need the Document and an OutputStream object write the content of the PDF we added to the Document into a PDF file.
  • We add a paragraph into the Document instance using the Paragraph object.
  • Finally, we need to close the document by calling the Document.close() method.

And here is a code example that demonstrate the creation of a PDF document.

package org.kodejava.itextpdf;

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

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

public class DocumentCreate {
    public static void main(String[] args) {
        // Create a new document.
        Document document = new Document();
        try {
            // Get an instance of PdfWriter and create a HelloWorld.pdf
            // file as an output.
            PdfWriter.getInstance(document,
                    new FileOutputStream("HelloWorld.pdf"));
            document.open();

            // Create our first paragraph for the pdf document to be
            // created. We also set the alignment and the font of the
            // paragraph.
            String text = "Kode Java website provides beginners to Java " +
                    "programming some examples to use the Java API " +
                    "(Application Programming Interface) to develop " +
                    "applications. Learning from some examples will " +
                    "hopefully decrease the time required to learn " +
                    "Java.";
            Paragraph paragraph = new Paragraph(text);
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFont(new Font(
                    Font.FontFamily.HELVETICA, 10, Font.NORMAL));

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

Maven Dependencies

<!-- https://search.maven.org/remotecontent?filepath=com/itextpdf/itextpdf/5.5.13.3/itextpdf-5.5.13.3.jar -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central