How do I create an underlined or strike through chunk in iText?

You can use the Chunk‘s setUnderline(float thickness, float yPosition) method to add underline or strike through to a chunk. A negative value to the yPosition create an underline while a positive value will produce a strike through.

package org.kodejava.example.itextpdf;

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

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

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

            // Creates a chunk with an underline with 0.1 thickness
            Chunk underline = new Chunk("The quick brown fox ");
            underline.setUnderline(0.1f, -1f);
            doc.add(underline);

            // Creates a strike through chunk with 1 thickness
            Chunk strike = new Chunk("jumps over the lazy dog.");
            strike.setUnderline(1f, 3f);
            doc.add(strike);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>

How do I create super / subscript in iText?

The following example you’ll see how to create a superscript and subscript text in the pdf document using iText. We can use the Chunk‘s class method called setTextRise(). A positive value will create a superscript text while a negative value will create a subscript text.

package org.kodejava.example.itextpdf;

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

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

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

            Font small = FontFactory.getFont(FontFactory.HELVETICA, 5, Font.ITALIC);

            // Add some chunks into the doc object.
            doc.add(new Chunk("The quick brown  "));

            Chunk superscript = new Chunk("fox ");
            superscript.setTextRise(5f);
            superscript.setFont(small);
            doc.add(superscript);

            doc.add(new Chunk("jumps over the lazy "));

            Chunk subscript = new Chunk("dog");
            subscript.setTextRise(-5f);
            subscript.setFont(small);
            doc.add(subscript);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>

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.example.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

<!-- http://repo1.maven.org/maven2/com/itextpdf/itextpdf/5.5.10/itextpdf-5.5.10.jar -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>