In certain circumstances, you may need to insert superscripts and subscripts in Microsoft Word. For instance, when you are creating an academic document involving scientific formulas. In this article, you will learn how to insert superscripts and subscripts into Word documents in Java using Spire.Doc for Java library.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.11.2</version>
</dependency>
</dependencies>
Insert Superscripts and Subscripts into Word using Java
The following are the main steps to insert a superscript or subscript into a Word document using Spire.Doc for Java:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Get the specific section using Document.getSections().get(sectionIndex) method.
- Add a paragraph to the section using Section.addParagraph() method.
- Add normal text to the paragraph using Paragraph.appendText() method.
- Add superscript or subscript text to the paragraph using Paragraph.appendText() method.
- Apply superscript or subscript formatting to the superscript or subscript text through TextRange.getCharacterFormat().setSubSuperScript() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SubSuperScript;
import com.spire.doc.fields.TextRange;
public class InsertSuperscriptAndSubscript {
public static void main(String[] args){
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("Sample.docx");
//Get the first section
Section section = document.getSections().get(0);
//Add a paragraph to the section
Paragraph paragraph = section.addParagraph();
//Add normal text to the paragraph
paragraph.appendText("E = mc");
//Add superscript text to the paragraph
TextRange superscriptText = paragraph.appendText("2");
//Apply superscript formatting to the superscript text
superscriptText.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
//Start a new line
paragraph.appendBreak(BreakType.Line_Break);
//Add normal text to the paragraph
paragraph.appendText("H");
//Add subscript text to the paragraph
TextRange subscriptText = paragraph.appendText("2");
//Apply subscript formatting to the subscript text
subscriptText.getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
//Add normal text to the paragraph
paragraph.appendText("O");
//Set font size for the text in the paragraph
for(Object item : paragraph.getItems())
{
if (item instanceof TextRange)
{
TextRange textRange = (TextRange)item ;
textRange.getCharacterFormat().setFontSize(36f);
}
}
//Save the result document
document.saveToFile("InsertSuperscriptAndSubscript.docx", FileFormat.Docx_2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
