上下标常见于数学公式、化学表达式等科学公式中。使用spire.doc时,用户可以通过characterformat类中subsuperscript属性来将字符(串)设置为上标或下标。
c#
//初始化document对象,添加section和段落
document doc = new document();
section sec = doc.addsection();
paragraph para = sec.addparagraph();
//写入钠与水反应的化学反应方程式
para.appendtext("2na 2h");
para.appendtext("2").characterformat.subsuperscript = subsuperscript.subscript;//2作为下标
para.appendtext("=2naoh h");
para.appendtext("2").characterformat.subsuperscript = subsuperscript.subscript;//2作为下标
para.appendbreak(breaktype.linebreak);//换行
//写入公式a^2 b^2=c^2
para.appendtext("a");
para.appendtext("2").characterformat.subsuperscript = subsuperscript.superscript;//2作为上标
para.appendtext(" b");
para.appendtext("2").characterformat.subsuperscript = subsuperscript.superscript;//2作为上标
para.appendtext("=c");
para.appendtext("2").characterformat.subsuperscript = subsuperscript.superscript;//2作为上标
para.appendbreak(breaktype.linebreak);//换行
//写入公式an=sn-sn-1
para.appendtext("an");
para.appendtext("=s");
para.appendtext("n").characterformat.subsuperscript = subsuperscript.subscript;//n作为下标
para.appendtext("-s");
para.appendtext("n-1").characterformat.subsuperscript = subsuperscript.subscript;//n-1作为下标
//设置字体大小
foreach (var i in para.items)
{
if (i is textrange)
{
(i as textrange).characterformat.fontsize = 20;
}
}
//保存文档
doc.savetofile("output.docx");
vb.net
'初始化document对象,添加section和段落
dim doc as document = new document
dim sec as section = doc.addsection
dim para as paragraph = sec.addparagraph
'写入钠与水反应的化学反应方程式
para.appendtext("2na 2h")
para.appendtext("2").characterformat.subsuperscript = subsuperscript.subscript'2作为下标
para.appendtext("=2naoh h")
para.appendtext("2").characterformat.subsuperscript = subsuperscript.subscript'2作为下标
para.appendbreak(breaktype.linebreak)'换行
'写入公式a^2 b^2=c^2
para.appendtext("a")
para.appendtext("2").characterformat.subsuperscript = subsuperscript.superscript'2作为上标
para.appendtext(" b")
para.appendtext("2").characterformat.subsuperscript = subsuperscript.superscript'2作为上标
para.appendtext("=c")
para.appendtext("2").characterformat.subsuperscript = subsuperscript.superscript'2作为上标
para.appendbreak(breaktype.linebreak)'换行
'写入公式an=sn-sn-1
para.appendtext("an")
para.appendtext("=s")
para.appendtext("n").characterformat.subsuperscript = subsuperscript.subscript'n作为下标
para.appendtext("-s")
para.appendtext("n-1").characterformat.subsuperscript = subsuperscript.subscript'n-1作为下标
'设置字体大小
for each i in para.items
if (typeof i is textrange) then
ctype(i,textrange).characterformat.fontsize = 20
end if
next
'保存文档
doc.savetofile("output.docx")