脚注,是可以附在文章页面的最底端的,对某些东西加以说明,印在书页下端的注文。脚注和尾注是对文本的补充说明。脚注一般位于页面的底部,可以作为文档某处内容的注释。常用在一些说明书、标书、论文等正式文书用来引注的内容。这篇文章将为您展示如何通过C#/VB.NET代码,以编程方式在Word中插入或删除脚注。以下是我整理的具体步骤及方法,并附上C#/VB.NET代码供大家参考。

  • 在Word中的特定段落后插入脚注
  • 在Word中的特定文本后插入脚注
  • 删除Word文档中的脚注

程序环境

本次测试时,在程序中引入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中的特定段落后插入脚注

以下是在指定段落后插入脚注的详细步骤。

  • 创建Document实例
  • 使用Document.LoadFromFile() 方法加载示例Word文档。
  • 获取第一节,然后获取该节中的指定段落。
  • 使用Paragraph.AppendFootnote(FootnoteType.Footnote) 方法在段落末尾添加脚注。
  • 设置脚注的文本内容、字体和颜色,然后设置脚注上标数字的格式。
  • 使用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)
        {
            //创建Document实例
            Document document = new Document();

            //加载Word文档示例
            document.LoadFromFile("我与地坛.docx");

            //获取第一节
            Section section = document.Sections[0];

            //获取节中的指定段落
            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;

            //设置脚注上标数字的格式
            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())
            '创建Document实例
            Dim document As Document = New Document()

            '加载Word文档示例
            document.LoadFromFile("我与地坛.docx")

            '获取第一节
            Dim section As Section = document.Sections(0)

            '获取节中的指定段落
            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

            '设置脚注上标数字的格式
            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

效果图

C#/VB.NET实现在Word中插入或删除脚注

在Word中的特定文本后插入脚注

我们还可以在文档中任何位置的指定文本后插入脚注。以下是详细步骤。

  • 创建Document实例。
  • 使用Document.LoadFromFile() 方法加载Word文档。
  • 使用Document.FindString() 方法查找指定的文本。
  • 使用TextSelection.GetAsOneRange() 方法获取指定文本的文本范围。
  • 使用TextRange.OwnerParagraph 属性获取文本范围所在的段落。
  • 使用Paragraph.ChildObjects.IndexOf() 方法获取段落中文本范围的位置索引。
  • 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法添加脚注,然后使用Paragraph.ChildObjects.Insert() 方法在指定文本后插入脚注
  • 设置脚注的文本内容、字体和颜色,然后设置脚注上标数字的格式。
  • 使用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)
        {
            //创建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);
        }
    }
}

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())
            '创建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

效果图

C#/VB.NET实现在Word中插入或删除脚注

以上就是C#/VB.NET实现在Word中插入或删除脚注的详细内容,更多关于C# Word插入删除脚注的资料请关注其它相关文章!

原文地址:https://www.cnblogs.com/Carina-baby/p/17191420.html

相关文章: