本文将介绍通过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();
}
}
上下标设置效果: