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

2025-04-24 02:44:14

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、添加公式1paragraph.AppendText("E = mc");添加文本,并设置文本为上标TextRange r锾攒揉敫ange1 = paragraph.AppendText("2");range1.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SuperScript;

5、设置断行paragraph.AppendBreak(BreakType.LineBreak);

6、//添加公式2paragraph.AppendText("F");//添加并豹肉钕舞设置相应字符串为下标TextR锾攒揉敫ange 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。
猜你喜欢