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.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;

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

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

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

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

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

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

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

How do I create an underlined or striked 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 document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("UnderStrike.pdf"));
            document.open();

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

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