温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-03-09 14:06:32 来源:亿速云 阅读:74 作者:iii 栏目:开发技术

本篇内容介绍了“C#/VB.NET怎么实现在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中插入或删除脚注”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI