Word控件Spire.Doc 【脚注】教程(1) 使用C#或VB.NET在 Word 文档中插入脚注和尾注

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:767755948)https://www.evget.com/product/3368/download

脚注和尾注是简短的注释,可用于对文档中的某些单词或句子提供解释、注释或引用。脚注通常出现在包含其参考编号的页面底部,而尾注出现在文档或章节的末尾。如果您正在用 Word 撰写学术论文,则插入脚注或尾注可能是必不可少的。本文将演示如何用Spire.Doc for .NET.在 C# 和 VB.NET 的 Word 文档中插入脚注和尾注。

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从过以下方式安装 NuGet.

PM> Install-Package Spire.Doc

在 C# 和 VB.NET Word 中插入脚注

脚注由两部分组成 :脚注参考标记和相应的脚注文本。要插入特定文本的脚注,您需要搜索文本并获取文本所在的段落,然后在段落中添加脚注,然后在找到的文本后插入脚注引用标记并设置脚注文本。具体步骤如下:

  • 初始化文档
  • 使用加载 Word 文档 Document.LoadFromFile() 方法。
  • 使用 Document.FindString() 方法,并使用以下命令将找到的文本作为单个文本范围获取 TextSelection.GetAsOneRange() 方法。
  • 通过以下方式访问文本范围的所有者段落 TextRange.OwnerParagraph 属性,并使用 Paragraph.ChildObjects.IndexOf() 方法。
  • 使用在段落中添加脚注 Paragraph.AppendFootnote(FootnoteType.Footnote) 方法。
  • 在文本范围后插入脚注引用标记,使用 Paragraph.ChildObjects.Insert() 方法。
  • 使用设置脚注文本 Footnote.TextBody.AddParagraph().追加文本() 方法。
  • 设置脚注文本和参考标记的格式,例如字体名称、字体大小和文本颜色。
  • 保存结果文档使用 Document.SaveToFile() 方法。

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertFootnote
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"Sample.docx");

//Find a specific text in the document
TextSelection selection = document.FindString("Spire.Doc for .NET", false, true);
//Get the found text as a single text range
TextRange textRange = selection.GetAsOneRange();
//Get the owner paragraph of the text range
Paragraph paragraph = textRange.OwnerParagraph;
//Get the index of the text range in the paragraph
int index = paragraph.ChildObjects.IndexOf(textRange);

//Add a footnote to the paragraph
Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
//Insert the footnote reference mark after the text range
paragraph.ChildObjects.Insert(index + 1, footnote);
//Set the footnote text
textRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD.");

//Set format for the footnote text
textRange.CharacterFormat.FontName = "Arial Black";
textRange.CharacterFormat.FontSize = 12;
textRange.CharacterFormat.TextColor = Color.DarkGray;

//Set format for the footnote reference mark
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 12;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

//Save the result document
document.SaveToFile("InsertFootnote.docx", FileFormat.Docx2013);
document.Close();
}
}
}

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())
'Initialize an instance of the Document class
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")

'Find a specific text in the document
Dim selection As TextSelection = document.FindString("Spire.Doc for .NET", False, True)
'Get the found text as a single text range
Dim textRange As TextRange = selection.GetAsOneRange()
'Get the owner paragraph of the text range
Dim paragraph As Paragraph = textRange.OwnerParagraph
'Get the index of the text range in the paragraph
Dim index As Integer = paragraph.ChildObjects.IndexOf(textRange)

'Add a footnote to the paragraph
Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)
'Insert the footnote reference mark after the text range
paragraph.ChildObjects.Insert(index + 1, footnote)
'Set the footnote text
textRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD.")

'Set format for the footnote text
textRange.CharacterFormat.FontName = "Arial Black"
textRange.CharacterFormat.FontSize = 12
textRange.CharacterFormat.TextColor = Color.DarkGray

'Set format for the footnote reference mark
footnote.MarkerCharacterFormat.FontName = "Calibri"
footnote.MarkerCharacterFormat.FontSize = 12
footnote.MarkerCharacterFormat.Bold = True
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen

'Save the result document
document.SaveToFile("InsertFootnote.docx", FileFormat.Docx2013)
document.Close()
End Sub
End Class
End Namespace

在 C# 和 VB.NET 的 Word 中插入尾注

尾注由两部分组成 —— 尾注引用标记和相应的尾注文本。为特定文本插入尾注的步骤与上述示例非常相似:

  • 初始化文档。
  • 使用 加载 Word 文档 Document.LoadFromFile() 方法。
  • 使用 Document.FindString() 方法,并使用以下命令将找到的文本作为单个文本范围获取 TextSelection.GetAsOneRange() 方法。
  • 通过以下方式访问文本范围的所有者段落 TextRange.OwnerParagraph 属性,并使用 Paragraph.ChildObjects.IndexOf() 方法。
  • 使用 向段落添加尾注 Paragraph.AppendFootnote(FootnoteType.Endnote) 方法。
  • 在文本范围后插入尾注引用标记 Paragraph.ChildObjects.Insert() 方法。
  • 使用 设置尾注文本 Footnote.TextBody.AddParagraph().追加文本() 方法。
  • 设置尾注文本和参考标记的字体名称、字体大小和文本颜色等格式。
  • 保存结果文档使用 Document.SaveToFile() 方法。

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertEndnote
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"Sample.docx");

//Find a specific text in the document
TextSelection selection = document.FindString("Microsoft Office", false, true);
//Get the found text as a single text range
TextRange textRange = selection.GetAsOneRange();
//Get the owner paragraph of the text range
Paragraph paragraph = textRange.OwnerParagraph;
//Get the index of the text range in the paragraph
int index = paragraph.ChildObjects.IndexOf(textRange);

//Add an endnote to the paragraph
Footnote endnote = paragraph.AppendFootnote(FootnoteType.Endnote);
//Insert the endnote reference mark after the text range
paragraph.ChildObjects.Insert(index + 1, endnote);
//Set the endnote text
textRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft.");

//Set format for the endnote text
textRange.CharacterFormat.FontName = "Arial Black";
textRange.CharacterFormat.FontSize = 12;
textRange.CharacterFormat.TextColor = Color.DarkGray;

//Set format for the endnote reference mark
endnote.MarkerCharacterFormat.FontName = "Calibri";
endnote.MarkerCharacterFormat.FontSize = 12;
endnote.MarkerCharacterFormat.Bold = true;
endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

//Save the result document
document.SaveToFile("InsertEndnote.docx", FileFormat.Docx2013);
document.Close();
}
}
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

Namespace InsertEndnote
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Initialize an instance of the Document class
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")

'Find a specific text in the document
Dim selection As TextSelection = document.FindString("Microsoft Office", False, True)
'Get the found text as a single text range
Dim textRange As TextRange = selection.GetAsOneRange()
'Get the owner paragraph of the text range
Dim paragraph As Paragraph = textRange.OwnerParagraph
'Get the index of the text range in the paragraph
Dim index As Integer = paragraph.ChildObjects.IndexOf(textRange)

'Add a endnote to the paragraph
Dim endnote As Footnote = paragraph.AppendFootnote(FootnoteType.Endnote)
'Insert the endnote reference mark after the text range
paragraph.ChildObjects.Insert(index + 1, endnote)
'Set the endnote text
textRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft.")

'Set format for the endnote text
textRange.CharacterFormat.FontName = "Arial Black"
textRange.CharacterFormat.FontSize = 12
textRange.CharacterFormat.TextColor = Color.DarkGray

'Set format for the endnote reference mark
endnote.MarkerCharacterFormat.FontName = "Calibri"
endnote.MarkerCharacterFormat.FontSize = 12
endnote.MarkerCharacterFormat.Bold = True
endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen

'Save the result document
document.SaveToFile("InsertEndnote.docx", FileFormat.Docx2013)
document.Close()
End Sub
End Class
End Namespace

申请临时许可证

如果您想从生成的文档中删除评估消息,或摆脱功能限制,请 为自己申请 30 天试用许可证 。

以上便是如何使用C#或VB.NET:在 Word 文档中插入脚注和尾注的方法,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/130080235