翻譯|使用教程|編輯:胡濤|2023-05-30 11:41:09.560|閱讀 154 次
概述:在本文中,我們將演示如何使用Spire.Doc for .NET庫在 C# 和 VB.NET 中將上標和下標插入到 Word 中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
上標或下標是位于正常文本行上方或下方的字符。它們通常用于科學(xué)公式,例如數(shù)學(xué)方程式或化學(xué)表達式。如果您正在創(chuàng)建包含科學(xué)公式的文檔,您很可能需要插入上標或下標。在本文中,我們將演示如何使用Spire.Doc for .NET庫在 C# 和 VB.NET 中將上標和下標插入到 Word 中。
首先,您需要添加包含在 Spire.Doc for.NET 包中的 DLL 文件作為您的 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
以下是使用 Spire.Doc for .NET 將上標或下標插入 Word 文檔的主要步驟:
【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 中插入上標和下標,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn