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.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
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024