本文将介绍通过Spire.Doc for Java给Word文档中的指定字符或字符串设置上标或者下标的方法。
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,设置下标
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);
//保存文档
doc.saveToFile("SubSuperScript.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
上下标设置效果: