C# 设置Word字符串的上标、下标

2025-10-22 02:20:24

1、下载安装该类库,并在项目程序中添加引用Spire.Doc.dll(dll文件可以在安装路径下的Bin文件夹中获取)

C# 设置Word字符串的上标、下标

2、添加using指令

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

3、实例化Document类,加载word测试文档

Document document = new Document("test.docx");

Paragraph paragraph = document.LastSection.AddParagraph();

4、添加公式1

paragraph.AppendText("E = mc");

添加文本,并设置文本为上标

TextRange range1 = paragraph.AppendText("2");

range1.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SuperScript;

5、设置断行

paragraph.AppendBreak(BreakType.LineBreak);

6、//添加公式2

paragraph.AppendText("F");

//添加并设置相应字符串为下标

TextRange range2 = paragraph.AppendText("n");

range2.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript; 

paragraph.AppendText(" = F");

paragraph.AppendText("n-1").CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript;

paragraph.AppendText(" + F");

paragraph.AppendText("n-2").CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript;

7、设置公式字号大小

foreach (var i in paragraph.Items)

{

    if (i is TextRange)

    {

        (i as TextRange).CharacterFormat.FontSize = 30;

    }

}

8、保存并打开文档

document.SaveToFile("result.docx", FileFormat.Docx);

System.Diagnostics.Process.Start("result.docx");

9、运行程序后,生成文档。

C# 设置Word字符串的上标、下标

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢