C#/VB.NET實現(xiàn)在Word中插入或刪除腳注
腳注,是可以附在文章頁面的最底端的,對某些東西加以說明,印在書頁下端的注文。腳注和尾注是對文本的補充說明。腳注一般位于頁面的底部,可以作為文檔某處內容的注釋。常用在一些說明書、標書、論文等正式文書用來引注的內容。這篇文章將為您展示如何通過C#/VB.NET代碼,以編程方式在Word中插入或刪除腳注。以下是我整理的具體步驟及方法,并附上C#/VB.NET代碼供大家參考。
- 在Word中的特定段落后插入腳注
- 在Word中的特定文本后插入腳注
- 刪除Word文檔中的腳注
程序環(huán)境
本次測試時,在程序中引入Free Spire.Doc for .NET??赏ㄟ^以下方法引用 Free Spire.Doc.dll文件:
方法1:將 Free Spire.Doc for .NET下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的 Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標右鍵點擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2:通過NuGet安裝??赏ㄟ^以下2種方法安裝:
(1)可以在Visual Studio中打開“解決方案資源管理器”,鼠標右鍵點擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點擊“安裝”。等待程序安裝完成。
(2)將以下內容復制到PM控制臺安裝。
Install-Package FreeSpire.Doc -Version 10.8.0
在Word中的特定段落后插入腳注
以下是在指定段落后插入腳注的詳細步驟。
- 創(chuàng)建Document實例
- 使用Document.LoadFromFile() 方法加載示例Word文檔。
- 獲取第一節(jié),然后獲取該節(jié)中的指定段落。
- 使用Paragraph.AppendFootnote(FootnoteType.Footnote) 方法在段落末尾添加腳注。
- 設置腳注的文本內容、字體和顏色,然后設置腳注上標數(shù)字的格式。
- 使用Document.SaveToFile() 方法保存結果文檔。
完整代碼
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace AddFootnote { class Program { static void Main(string[] args) { //創(chuàng)建Document實例 Document document = new Document(); //加載Word文檔示例 document.LoadFromFile("我與地壇.docx"); //獲取第一節(jié) Section section = document.Sections[0]; //獲取節(jié)中的指定段落 Paragraph paragraph = section.Paragraphs[1]; //在段落末尾添加腳注 Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote); //設置腳注的文本內容 TextRange text = footnote.TextBody.AddParagraph().AppendText("即使世界毀滅,那又怎樣,天地崩塌,于我何干,我在乎的只有他。"); //設置文本字體和顏色 text.CharacterFormat.FontName = "宋體"; text.CharacterFormat.FontSize = 12; text.CharacterFormat.TextColor = Color.DarkBlue; //設置腳注上標數(shù)字的格式 footnote.MarkerCharacterFormat.FontName = "Calibri"; footnote.MarkerCharacterFormat.FontSize = 15; footnote.MarkerCharacterFormat.Bold = true; footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan; //保存結果文檔 document.SaveToFile("添加腳注.docx", FileFormat.Docx); } } }
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace AddFootnote Friend Class Program Private Shared Sub Main(ByVal args As String()) '創(chuàng)建Document實例 Dim document As Document = New Document() '加載Word文檔示例 document.LoadFromFile("我與地壇.docx") '獲取第一節(jié) Dim section As Section = document.Sections(0) '獲取節(jié)中的指定段落 Dim paragraph As Paragraph = section.Paragraphs(1) '在段落末尾添加腳注 Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote) '設置腳注的文本內容 Dim text As TextRange = footnote.TextBody.AddParagraph().AppendText("即使世界毀滅,那又怎樣,天地崩塌,于我何干,我在乎的只有他。") '設置文本字體和顏色 text.CharacterFormat.FontName = "宋體" text.CharacterFormat.FontSize = 12 text.CharacterFormat.TextColor = Color.DarkBlue '設置腳注上標數(shù)字的格式 footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 15 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan '保存結果文檔 document.SaveToFile("添加腳注.docx", FileFormat.Docx) End Sub End Class End Namespace
效果圖
在Word中的特定文本后插入腳注
我們還可以在文檔中任何位置的指定文本后插入腳注。以下是詳細步驟。
- 創(chuàng)建Document實例。
- 使用Document.LoadFromFile() 方法加載Word文檔。
- 使用Document.FindString() 方法查找指定的文本。
- 使用TextSelection.GetAsOneRange() 方法獲取指定文本的文本范圍。
- 使用TextRange.OwnerParagraph 屬性獲取文本范圍所在的段落。
- 使用Paragraph.ChildObjects.IndexOf() 方法獲取段落中文本范圍的位置索引。
- 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法添加腳注,然后使用Paragraph.ChildObjects.Insert() 方法在指定文本后插入腳注
- 設置腳注的文本內容、字體和顏色,然后設置腳注上標數(shù)字的格式。
- 使用Document.SaveToFile() 方法保存結果文檔。
完整代碼
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertFootnote { class Program { static void Main(string[] args) { //創(chuàng)建Document實例 Document document = new Document(); //加載Word文檔示例 document.LoadFromFile("我與地壇.docx"); //查找指定的文本字符串 TextSelection selection = document.FindString("最苦的母親", false, true); //獲取指定文本的文本范圍 TextRange textRange = selection.GetAsOneRange(); //獲取文本范圍所在的段落 Paragraph paragraph = textRange.OwnerParagraph; //獲取段落中文本范圍的位置索引 int index = paragraph.ChildObjects.IndexOf(textRange); //添加腳注 Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote); //在指定段落后插入腳注 paragraph.ChildObjects.Insert(index + 1, footnote); //設置腳注的文本內容 TextRange text = footnote.TextBody.AddParagraph().AppendText("不知道兒子的不幸在母親那兒總是要加倍的。"); //設置文本字體和顏色 text.CharacterFormat.FontName = "宋體"; text.CharacterFormat.FontSize = 12; text.CharacterFormat.TextColor = Color.DarkBlue; //設置腳注上標數(shù)字的格式 footnote.MarkerCharacterFormat.FontName = "Calibri"; footnote.MarkerCharacterFormat.FontSize = 15; footnote.MarkerCharacterFormat.Bold = true; footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen; //保存結果文檔 document.SaveToFile("插入腳注.docx", FileFormat.Docx); } } }
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertFootnote Friend Class Program Private Shared Sub Main(ByVal args As String()) '創(chuàng)建Document實例 Dim document As Document = New Document() '加載Word文檔示例 document.LoadFromFile("我與地壇.docx") '查找指定的文本字符串 Dim selection As TextSelection = document.FindString("最苦的母親", False, True) '獲取指定文本的文本范圍 Dim textRange As TextRange = selection.GetAsOneRange() '獲取文本范圍所在的段落 Dim paragraph As Paragraph = textRange.OwnerParagraph '獲取段落中文本范圍的位置索引 Dim index As Integer = paragraph.ChildObjects.IndexOf(textRange) '添加腳注 Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote) '在指定段落后插入腳注 paragraph.ChildObjects.Insert(index + 1, footnote) '設置腳注的文本內容 Dim text As TextRange = footnote.TextBody.AddParagraph().AppendText("不知道兒子的不幸在母親那兒總是要加倍的。") '設置文本字體和顏色 text.CharacterFormat.FontName = "宋體" text.CharacterFormat.FontSize = 12 text.CharacterFormat.TextColor = Color.DarkBlue '設置腳注上標數(shù)字的格式 footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 15 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen '保存結果文檔 document.SaveToFile("插入腳注.docx", FileFormat.Docx) End Sub End Class End Namespace
效果圖
以上就是C#/VB.NET實現(xiàn)在Word中插入或刪除腳注的詳細內容,更多關于C# Word插入刪除腳注的資料請關注腳本之家其它相關文章!
相關文章
C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法
某論壇的評論區(qū)模塊,發(fā)現(xiàn)這功能很不錯,琢磨了一晚上做了大致一樣的,用來當做 注冊模塊 的頭像綁定功能,下面通過實例代碼給大家介紹下C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法,感興趣的朋友一起看看吧2021-11-11C#使用OpenCvSharp4庫讀取電腦攝像頭數(shù)據(jù)并實時顯示
OpenCvSharp4庫是一個基于.Net封裝的OpenCV庫,本文主要給大家介紹了C#使用OpenCvSharp4庫讀取電腦攝像頭數(shù)據(jù)并實時顯示的詳細方法,感興趣的朋友可以參考下2024-01-01淺析C# 9.0 新特性之 Lambda 棄元參數(shù)
這篇文章主要介紹了C# 9.0 新特性之 Lambda 棄元參數(shù)的的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習,想學習c#的朋友可以了解下2020-06-06