乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > word下标-Spire.Doc系列教程:在Word文档中插入上下标并设置文字效果

word下标-Spire.Doc系列教程:在Word文档中插入上下标并设置文字效果

作者:乔山办公网日期:

返回目录:word文档

Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。以下示例将详细讲述如何使用Spire.Doc在Word文档中插入上下标并设置文字效果。




C# Word 插入上下标

上下标常见于数学公式、化学表达式等科学公式中。使用Spire.Doc时,用户可以通过CharacterFormat类中SubSuperScript属性来将字符(串)设置为上标或下标。


//初始化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^2para.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-1para.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");

效果图如下:


C# Word 中设置文字效果

下面这段示例将介绍如何使用Spire.Doc来设置文字效果,包括文本边框,文本填充,文字阴影等。


//实例化一个Document对象Document doc = new Document();//向文档中添加一个Section对象Section sec = doc.AddSection();//在这个section上添加一个段落并给文字加边框Paragraph p1 = sec.AddParagraph(); TextRange tr1 = p1.AppendText("加粉色边框的文字");tr1.CharacterFormat.Border.BorderType = BorderStyle.DashDotStroker;tr1.CharacterFormat.Border.Color = Color.Pink;p1.AppendBreak(BreakType.LineBreak);//添加一个新段落并设置文字填充效果Paragraph p2 = sec.AddParagraph();TextRange tr2 = p2.AppendText("设置填充效果的文字");//设置文字前景色tr2.CharacterFormat.TextColor = Color.Orange;//设置文字背景色tr2.CharacterFormat.TextBackgroundColor = Color.LightGray;//设置文字缩放比例tr2.CharacterFormat.TextScale = 150;p2.AppendBreak(BreakType.LineBreak);//添加一个新段落并设置阴影Paragraph p3 = sec.AddParagraph();TextRange tr3 = p3.AppendText("设置阴影效果的文字");tr3.CharacterFormat.TextColor = Color.LightSeaGreen;tr3.CharacterFormat.IsShadow = true;p3.AppendBreak(BreakType.LineBreak);//添加一个新段落并设置简单的文字样式Paragraph p4 = sec.AddParagraph();TextRange tr4 = p4.AppendText("设置删除线效果的文字");tr4.CharacterFormat.IsStrikeout = true;p4.AppendBreak(BreakType.LineBreak);TextRange tr5 = p4.AppendText("设置文字大写: hello, e-iceblue.");tr5.CharacterFormat.IsSmallCaps = true;//使用ClearFormatting()来删除某个TextRange的文字效果//tr5.CharacterFormat.ClearFormatting();//保存文档 doc.SaveToFile("文字效果.docx");

效果图如下:


如果你有任何问题或意见,或者想要获得更多教程资源,可在下方评论区留言哦~


相关阅读

关键词不能为空
极力推荐
  • Word也可以添加背景音乐?-word背景

  • word背景,Word添加背景音乐的功能藏得比较深,需要用到“开发工具”。但默认情况下,Word菜单栏中是没有该功能的,因此,首先得调出“开发工具”选项卡。

ppt怎么做_excel表格制作_office365_word文档_365办公网