本文介绍如何使用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);
}