How to superscript text example in PDF using java iText library

In this tutorial you will learn to add the subscript text in any paragraph or chunk of PDF document. The subscript text represents a character or text that is half the height of a normal character and printed at the down middle of the normal characters. The subscript text should be halfway down the normal text.

Please note we are using the iText version 5 for this example.

You can see in the below example that normal text can be created by Chunk element. For subscript text you can create the text using Chunk element with setting rise value as negative in method setTextRise();

Below is example of code which generates the PDF document with subscript text.

package com.java.connect.itext;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFSubScriptExample {
 
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        // Create new PDF document
        Document document = new Document();
        // Create an output stream of PDF file.
        OutputStream out = new FileOutputStream("PDF_Sub_Script_Example.pdf");
        // Get a PdfWriter instance to write in PDF document.
        PdfWriter.getInstance(document, out);
        // Open the PDF document
        document.open();
        // Create a chunk of text with normal Text rise.
        Chunk normalText = new Chunk("This is normal text.");
        // Add the normal text to PDF document.
        document.add(normalText);
        // Create a another Chunk of text with text rise of 6f
        Chunk subScript = new Chunk("This is Subcript");
        subScript.setTextRise(-6f);
        // Add the sub script to PDF document
        document.add(subScript);
        // Close the PDF documetn after use.
        document.close();
 
    }
 
}

The generated PDF document looks like below screen.

iText-sub-script-text-example-in-PDF-using-java-iText-library_files