Java sets up and subscripts in Word

Superscript refers to text that is slightly higher than other text in the same line, and subscript refers to text that is slightly lower than other text in the same line. In daily life, our common symbols such as square meters and cubic meters, as well as various element symbols in chemistry, are represented by superscripts and subscripts. This article will introduce how to set superscript or subscript to specified character or string in Word document through Free Spire.Doc for Java.

 

Jar package import

Method 1: Download the Free Spire.Doc for Java package and decompress it, and then import the Spire.Doc.jar package in the lib folder as a dependency into the Java application.

Method 2: Install the JAR package through the Maven warehouse, and configure the code of the pom.xml file as follows:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>2.7.3</version>
    </dependency>
</dependencies>

Java code

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SubSuperScript;

public class SubSuperScritp {
    public static void main(String[] args) {
        //创建文档
        Document doc = new Document();
        Section sec = doc.addSection();

        //添加段落1,设置上标
        Paragraph para1 = sec.addParagraph();
        para1.appendText("A");
        para1.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        para1.appendText("+B");
        para1.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        para1.appendText("=C");
        para1.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);

        //添加段落2、3,设置下标
        Paragraph para2 = sec.addParagraph();
        para2.appendText("An = S");
        para2.appendText("n").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
        para2.appendText("- S");
        para2.appendText("n-1").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
        Paragraph para3 = sec.addParagraph();
        para3.appendText("C");
        para3.appendText("O");
        para3.appendText("2").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);



        //保存文档
        doc.saveToFile("SubSuperScript.docx", FileFormat.Docx_2013);
        doc.dispose();
    }
}

 

Superscript and subscript setting effect:

 

Guess you like

Origin blog.csdn.net/a159357445566/article/details/108602757