Word控件Spire.Doc 【其他】教程(4):在 Word 中插入上标和下标

您所在的位置:网站首页 word中上标下标是什么 Word控件Spire.Doc 【其他】教程(4):在 Word 中插入上标和下标

Word控件Spire.Doc 【其他】教程(4):在 Word 中插入上标和下标

2023-06-13 12:16| 来源: 网络整理| 查看: 265

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.Doc for.NET 最新下载(qun:761297826)icon-default.png?t=N4P3https://www.evget.com/product/3368/download

上标或下标是位于正常文本行上方或下方的字符。它们通常用于科学公式,例如数学方程式或化学表达式。如果您正在创建包含科学公式的文档,您很可能需要插入上标或下标。在本文中,我们将演示如何使用Spire.Doc for .NET库在 C# 和 VB.NET 中将上标和下标插入到 Word 中。

安装适用于 .NET 的 Spire.Doc

首先,您需要添加包含在 Spire.Doc for.NET 包中的 DLL 文件作为您的 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.Doc

使用 C# 和 VB.NET 将上标和下标插入到 Word 中

以下是使用 Spire.Doc for .NET 将上标或下标插入 Word 文档的主要步骤:

创建一个文档实例。使用Document.LoadFromFile()方法加载 Word 文档。通过Document.Sections[sectionIndex]属性获取具体的节。使用Section.AddParagraph()方法向节中添加一个段落。使用Paragraph.AppendText()方法向段落添加普通文本。使用Paragraph.AppendText()方法向段落添加上标或下标文本。通过TextRange.CharacterFormat.SubSuperScript属性将上标或下标格式应用于上标或下标文本。使用Document.SaveToFile()方法保存结果文档。

【C#】

using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace InsertSuperscriptAndSubscript { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.LoadFromFile("Sample.docx"); //Get the first section Section section = document.Sections[0]; //Add a paragraph to the section Paragraph paragraph = section.AddParagraph(); //Add normal text to the paragraph paragraph.AppendText("E = mc"); //Add superscript text to the paragraph TextRange superscriptText = paragraph.AppendText("2"); //Apply superscript formatting to the superscript text superscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript; //Start a new line paragraph.AppendBreak(BreakType.LineBreak); //Add normal text to the paragraph paragraph.AppendText("H"); //Add subscript text to the paragraph TextRange subscriptText = paragraph.AppendText("2"); //Apply subscript formatting to the subscript text subscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SubScript; //Add normal text to the paragraph paragraph.AppendText("O"); //Set font size for the text in the paragraph foreach (var item in paragraph.Items) { if (item is TextRange) { TextRange textRange = item as TextRange; textRange.CharacterFormat.FontSize = 36f; } } //Save the result document document.SaveToFile("InsertSuperscriptAndSubscript.docx", FileFormat.Docx2013); } } }

【VB.NET】

Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace InsertSuperscriptAndSubscript Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Add a paragraph to the section Dim paragraph As Paragraph = section.AddParagraph() 'Add normal text to the paragraph paragraph.AppendText("E = mc") 'Add superscript text to the paragraph Dim superscriptText As TextRange = paragraph.AppendText("2") 'Apply superscript formatting to the superscript text superscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript 'Start a new line paragraph.AppendBreak(BreakType.LineBreak) 'Add normal text to the paragraph paragraph.AppendText("H") 'Add subscript text to the paragraph Dim subscriptText As TextRange = paragraph.AppendText("2") 'Apply subscript formatting to the subscript text subscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SubScript 'Add normal text to the paragraph paragraph.AppendText("O") 'Set font size for the text in the paragraph For Each item In paragraph.Items If TypeOf item Is TextRange Then Dim textRange As TextRange = TryCast(item, TextRange) textRange.CharacterFormat.FontSize = 36F End If Next 'Save the result document document.SaveToFile("InsertSuperscriptAndSubscript.docx", FileFormat.Docx2013) End Sub End Class End Namespace

以上便是在 Word 中插入上标和下标,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3