C#/VB.NET實(shí)現(xiàn)在Word中插入或刪除腳注
腳注,是可以附在文章頁(yè)面的最底端的,對(duì)某些東西加以說(shuō)明,印在書(shū)頁(yè)下端的注文。腳注和尾注是對(duì)文本的補(bǔ)充說(shuō)明。腳注一般位于頁(yè)面的底部,可以作為文檔某處內(nèi)容的注釋。常用在一些說(shuō)明書(shū)、標(biāo)書(shū)、論文等正式文書(shū)用來(lái)引注的內(nèi)容。這篇文章將為您展示如何通過(guò)C#/VB.NET代碼,以編程方式在Word中插入或刪除腳注。以下是我整理的具體步驟及方法,并附上C#/VB.NET代碼供大家參考。
- 在Word中的特定段落后插入腳注
- 在Word中的特定文本后插入腳注
- 刪除Word文檔中的腳注
程序環(huán)境
本次測(cè)試時(shí),在程序中引入Free Spire.Doc for .NET??赏ㄟ^(guò)以下方法引用 Free Spire.Doc.dll文件:
方法1:將 Free Spire.Doc for .NET下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的 Spire.Doc.dll。然后在Visual Studio中打開(kāi)“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2:通過(guò)NuGet安裝。可通過(guò)以下2種方法安裝:
(1)可以在Visual Studio中打開(kāi)“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點(diǎn)擊“安裝”。等待程序安裝完成。
(2)將以下內(nèi)容復(fù)制到PM控制臺(tái)安裝。
Install-Package FreeSpire.Doc -Version 10.8.0
在Word中的特定段落后插入腳注
以下是在指定段落后插入腳注的詳細(xì)步驟。
- 創(chuàng)建Document實(shí)例
- 使用Document.LoadFromFile() 方法加載示例Word文檔。
- 獲取第一節(jié),然后獲取該節(jié)中的指定段落。
- 使用Paragraph.AppendFootnote(FootnoteType.Footnote) 方法在段落末尾添加腳注。
- 設(shè)置腳注的文本內(nèi)容、字體和顏色,然后設(shè)置腳注上標(biāo)數(shù)字的格式。
- 使用Document.SaveToFile() 方法保存結(jié)果文檔。
完整代碼
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實(shí)例
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);
//設(shè)置腳注的文本內(nèi)容
TextRange text = footnote.TextBody.AddParagraph().AppendText("即使世界毀滅,那又怎樣,天地崩塌,于我何干,我在乎的只有他。");
//設(shè)置文本字體和顏色
text.CharacterFormat.FontName = "宋體";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkBlue;
//設(shè)置腳注上標(biāo)數(shù)字的格式
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 15;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan;
//保存結(jié)果文檔
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實(shí)例
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)
'設(shè)置腳注的文本內(nèi)容
Dim text As TextRange = footnote.TextBody.AddParagraph().AppendText("即使世界毀滅,那又怎樣,天地崩塌,于我何干,我在乎的只有他。")
'設(shè)置文本字體和顏色
text.CharacterFormat.FontName = "宋體"
text.CharacterFormat.FontSize = 12
text.CharacterFormat.TextColor = Color.DarkBlue
'設(shè)置腳注上標(biāo)數(shù)字的格式
footnote.MarkerCharacterFormat.FontName = "Calibri"
footnote.MarkerCharacterFormat.FontSize = 15
footnote.MarkerCharacterFormat.Bold = True
footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan
'保存結(jié)果文檔
document.SaveToFile("添加腳注.docx", FileFormat.Docx)
End Sub
End Class
End Namespace效果圖

在Word中的特定文本后插入腳注
我們還可以在文檔中任何位置的指定文本后插入腳注。以下是詳細(xì)步驟。
- 創(chuàng)建Document實(shí)例。
- 使用Document.LoadFromFile() 方法加載Word文檔。
- 使用Document.FindString() 方法查找指定的文本。
- 使用TextSelection.GetAsOneRange() 方法獲取指定文本的文本范圍。
- 使用TextRange.OwnerParagraph 屬性獲取文本范圍所在的段落。
- 使用Paragraph.ChildObjects.IndexOf() 方法獲取段落中文本范圍的位置索引。
- 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法添加腳注,然后使用Paragraph.ChildObjects.Insert() 方法在指定文本后插入腳注
- 設(shè)置腳注的文本內(nèi)容、字體和顏色,然后設(shè)置腳注上標(biāo)數(shù)字的格式。
- 使用Document.SaveToFile() 方法保存結(jié)果文檔。
完整代碼
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實(shí)例
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);
//設(shè)置腳注的文本內(nèi)容
TextRange text = footnote.TextBody.AddParagraph().AppendText("不知道兒子的不幸在母親那兒總是要加倍的。");
//設(shè)置文本字體和顏色
text.CharacterFormat.FontName = "宋體";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkBlue;
//設(shè)置腳注上標(biāo)數(shù)字的格式
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 15;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;
//保存結(jié)果文檔
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實(shí)例
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)
'設(shè)置腳注的文本內(nèi)容
Dim text As TextRange = footnote.TextBody.AddParagraph().AppendText("不知道兒子的不幸在母親那兒總是要加倍的。")
'設(shè)置文本字體和顏色
text.CharacterFormat.FontName = "宋體"
text.CharacterFormat.FontSize = 12
text.CharacterFormat.TextColor = Color.DarkBlue
'設(shè)置腳注上標(biāo)數(shù)字的格式
footnote.MarkerCharacterFormat.FontName = "Calibri"
footnote.MarkerCharacterFormat.FontSize = 15
footnote.MarkerCharacterFormat.Bold = True
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen
'保存結(jié)果文檔
document.SaveToFile("插入腳注.docx", FileFormat.Docx)
End Sub
End Class
End Namespace效果圖

以上就是C#/VB.NET實(shí)現(xiàn)在Word中插入或刪除腳注的詳細(xì)內(nèi)容,更多關(guān)于C# Word插入刪除腳注的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法
某論壇的評(píng)論區(qū)模塊,發(fā)現(xiàn)這功能很不錯(cuò),琢磨了一晚上做了大致一樣的,用來(lái)當(dāng)做 注冊(cè)模塊 的頭像綁定功能,下面通過(guò)實(shí)例代碼給大家介紹下C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法,感興趣的朋友一起看看吧2021-11-11
C#調(diào)用OutLokk實(shí)現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了如何利用C#調(diào)用OutLokk實(shí)現(xiàn)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
探討:如何使用委托,匿名方法對(duì)集合進(jìn)行萬(wàn)能排序
本篇文章是對(duì)使用委托,匿名方法對(duì)集合進(jìn)行萬(wàn)能排序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C#實(shí)現(xiàn).net頁(yè)面之間傳值傳參方法匯總
這篇文章主要介紹了C#實(shí)現(xiàn).net頁(yè)面之間傳值傳參方法,實(shí)例匯總了幾類常見(jiàn)的傳值傳參的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
C#使用OpenCvSharp4庫(kù)讀取電腦攝像頭數(shù)據(jù)并實(shí)時(shí)顯示
OpenCvSharp4庫(kù)是一個(gè)基于.Net封裝的OpenCV庫(kù),本文主要給大家介紹了C#使用OpenCvSharp4庫(kù)讀取電腦攝像頭數(shù)據(jù)并實(shí)時(shí)顯示的詳細(xì)方法,感興趣的朋友可以參考下2024-01-01
淺析C# 9.0 新特性之 Lambda 棄元參數(shù)
這篇文章主要介紹了C# 9.0 新特性之 Lambda 棄元參數(shù)的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),想學(xué)習(xí)c#的朋友可以了解下2020-06-06

