服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#/VB.NET实现在Word中插入或删除脚注

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

2023-03-09 16:17Carina-baby C#

脚注,是可以附在文章页面的最底端的,对某些东西加以说明,印在书页下端的注文。这篇文章将为您展示如何通过C#/VB.NET代码,以编程方式在Word中插入或删除脚注,需要的可以参考一下

脚注,是可以附在文章页面的最底端的,对某些东西加以说明,印在书页下端的注文。脚注和尾注是对文本的补充说明。脚注一般位于页面的底部,可以作为文档某处内容的注释。常用在一些说明书、标书、论文等正式文书用来引注的内容。这篇文章将为您展示如何通过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#

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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#

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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

延伸 · 阅读

精彩推荐
  • C#C# 中的 is 真的是越来越强大越来越语义化(推荐)

    C# 中的 is 真的是越来越强大越来越语义化(推荐)

    这篇文章主要介绍了C# 中的 is 真的是越来越强大越来越语义化,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可...

    一线码农5432022-10-10
  • C#C#实现文本转语音功能

    C#实现文本转语音功能

    这篇文章主要为大家详细介绍了C#实现文本转语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    qq_329153378472023-02-24
  • C#C#中List和SortedList的简介

    C#中List和SortedList的简介

    今天小编就为大家分享一篇关于C#中List和SortedList的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Czhenya6962022-03-03
  • C#浅析c# 接口

    浅析c# 接口

    这篇文章主要介绍了c# 接口的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下。...

    莫得感情的代码机器4762022-09-27
  • C#Unity UI实现循环播放序列图

    Unity UI实现循环播放序列图

    这篇文章主要为大家详细介绍了Unity UI实现循环播放序列图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    PangCoder4802022-11-29
  • C#C#使用Jquery zTree实现树状结构显示 异步数据加载

    C#使用Jquery zTree实现树状结构显示 异步数据加载

    这篇文章主要为大家详细介绍了C#使用Jquery zTree实现树状结构显示和异步数据加载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    深入学习ing4872021-12-15
  • C#C#中结构体定义并转换字节数组详解

    C#中结构体定义并转换字节数组详解

    在写C#TCP通信程序时,发送数据时,只能发送byte数组,处理起来比较麻烦不说,如果是和VC6.0等写的程序通信的话,很多的都是传送结构体,在VC6.0中可以很方便的把...

    dafanjoy4772022-02-10
  • C#轻松学习C#的读写操作

    轻松学习C#的读写操作

    轻松学习C#的读写操作,小编也是第一次接触C#的读写操作,感兴趣的小伙伴们可以参考一下,大家一起学习。...

    丿木呈广予口贝8762021-11-03