本文介绍如何使用Spire.Doc for Java在Word文档中添加上角标和下角标。
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 InsertSupSubScript {
public static void main(String[] args) {
//创建Document对象
Document document = new Document();
//添加节
Section section = document.addSection();
//添加段落
Paragraph paragraph = section.addParagraph();
//添加文字到段落
paragraph.appendText("E = mc");
//添加文字并设置为上角标
TextRange textRange = paragraph.appendText("2");
textRange.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
//添加换行
paragraph.appendBreak(BreakType.Line_Break);
//添加文字并设置部分文字为下角标
paragraph.appendText("a");
paragraph.appendText("n").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
paragraph.appendText(" = S");
paragraph.appendText("n").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
paragraph.appendText(" - S");
paragraph.appendText("n-1").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
//保存文档
document.saveToFile("InsertSubSuperScript.docx", FileFormat.Docx);
}