脚注是放置在页面底部的注释。在 MS Word 中,您可以在不影响正文的情况下使用脚注来引用参考文献、给出解释或发表评论。在本文中,您将学习如何使用 Spire.Doc for .NET 在 Word 文档中插入或删除脚注。
安装 Spire.Doc for .NET
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.Doc
在 Word 中的特定段落后插入脚注
Spire.Doc for .NET 提供的 Paragraph.AppendFootnote(FootnoteType.Footnote) 方法允许您在指定段落后插入脚注。以下是详细步骤。
- 创建 Document 实例
- 使用 Document.LoadFromFile() 方法加载示例 Word 文档。
- 获取第一节,然后获取该节中的指定段落。
- 使用 Paragraph.AppendFootnote(FootnoteType.Footnote) 方法在段落末尾添加脚注。
- 设置脚注的文本内容、字体和颜色,然后设置脚注上标数字的格式。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace AddFootnote
{
class Program
{
static void Main(string[] args)
{
//创建Document实例
Document document = new Document();
//加载Word文档示例
document.LoadFromFile("活着.docx");
//获取第一节
Section section = document.Sections[0];
//获取节中的指定段落
Paragraph paragraph = section.Paragraphs[2];
//在段落末尾添加脚注
Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
//设置脚注的文本内容
TextRange text = footnote.TextBody.AddParagraph().AppendText("祸兮福所倚,福兮祸所伏。人终归是要过完也只能过完自己注定的一生,活着的意义,便在于活着。");
//设置文本字体和颜色
text.CharacterFormat.FontName = "宋体";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkBlue;
//设置脚注上标数字的格式
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 15;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan;
//保存结果文档
document.SaveToFile("添加脚注.docx", FileFormat.Docx);
}
}
}
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())
'创建Document实例 Dim document As Document = New Document() '加载Word文档示例
document.LoadFromFile("活着.docx")
'获取第一节 Dim section As Section = document.Sections(0) '获取节中的指定段落
Dim paragraph As Paragraph = section.Paragraphs(2)
'在段落末尾添加脚注 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 '设置脚注上标数字的格式
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 中的特定文本后插入脚注
使用 Spire.Doc for .NET,您还可以在文档中任何位置的指定文本后插入脚注。以下是详细步骤。
- 创建 Document实例。
- 使用 Document.LoadFromFile()方法加载 Word 文档。
- 使用 Document.FindString()方法查找指定的文本。
- 使用 TextSelection.GetAsOneRange()方法获取指定文本的文本范围。
- 使用 TextRange.OwnerParagraph属性获取文本范围所在的段落。
- 使用 Paragraph.ChildObjects.IndexOf()方法获取段落中文本范围的位置索引。
- 使用 Paragraph.AppendFootnote(FootnoteType.Footnote)方法添加脚注,然后使用 Paragraph.ChildObjects.Insert()方法在指定文本后插入脚注
- 设置脚注的文本内容、字体和颜色,然后设置脚注上标数字的格式。
- 使用 Document.SaveToFile()方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.Drawing;namespace InsertFootnote{class Program{static void Main(string[] args){//创建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;//设置脚注上标数字的格式 footnote.MarkerCharacterFormat.FontName = "Calibri";footnote.MarkerCharacterFormat.FontSize = 15;footnote.MarkerCharacterFormat.Bold = true;footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;//保存结果文档 document.SaveToFile("插入脚注.docx",FileFormat.Docx);}}}
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()) '创建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 '设置脚注上标数字的格式
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
删除 Word 文档中的脚注
手动搜索和删除文档中的现有脚注需要花费大量时间和精力。以下是以编程方式一次删除所有脚注的步骤。
- 创建 Document实例。
- 使用 Document.LoadFromFile()方法加载 Word 文档。
- 使用 Document.Sections属性获取指定的节。
- 遍历该节中的每个段落以找到脚注。
- 使用 Paragraph.ChildObjects.RemoveAt()方法删除脚注。
- 使用 Document.SaveToFile()方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace RemoveFootnote{class Program{static void Main(string[] args){//创建Document实例 Document document = new Document();//加载Word文档示例 document.LoadFromFile("添加脚注.docx");//获取第一节 Section section = document.Sections[0];//遍历该节中的每个段落以找到脚注 foreach (Paragraph para in section.Paragraphs){int index = -1;for (int i = 0,cnt = para.ChildObjects.Count;i < cnt;i++){ParagraphBase pBase = para.ChildObjects[i] as ParagraphBase;if (pBase is Footnote){index = i;break;}}if (index>-1) //删除脚注 para.ChildObjects.RemoveAt(index);}//保存结果文档 document.SaveToFile("删除脚注.docx",FileFormat.Docx);}}}
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace RemoveFootnote Friend Class Program Private Shared Sub Main(ByVal args As String()) '创建Document实例
Dim document As Document = New Document()
'加载Word文档示例 document.LoadFromFile("添加脚注.docx") '获取第一节
Dim section As Section = document.Sections(0)
'遍历该节中的每个段落以找到脚注 For Each para As Paragraph In section.Paragraphs Dim index = -1 Dim i = 0,cnt As Integer = para.ChildObjects.Count While i < cnt Dim pBase As ParagraphBase = TryCast(para.ChildObjects(i),ParagraphBase) If TypeOf pBase Is Footnote Then index = i Exit While End If i += 1 End While '删除脚注
If index > -1 Then para.ChildObjects.RemoveAt(index)
Next
'保存结果文档 document.SaveToFile("删除脚注.docx",FileFormat.Docx) End Sub End Class End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。