C# /VB.NET添加、读取Word脚注/尾注

发布时间:2020-07-18 05:59:02 作者:E_iceblue
来源:网络 阅读:877

前言

脚注和尾注是对文本的补充说明。脚注一般位于页面的底部,可以作为文档某处内容的注释;尾注一般位于文档的末尾,列出引文 的出处等。在本示例中将介绍如何来添加或删除Word脚注。

工具使用

Free Spire. Doc for .NET

第一步 dll引用

C# /VB.NET添加、读取Word脚注/尾注

第二步 添加Word脚注、尾注

【C#】

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

namespace InsertFootnote_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个word文档对象并加载需要添加脚注尾注的word文档
            Document document = new Document();
            document.LoadFromFile("sample.docx", FileFormat.Docx2010);

            //获取第3个段落
            Paragraph paragraph = document.Sections[0].Paragraphs[2];

            //添加脚注
            Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);

            //查找指定字符串,并添加脚注
            DocumentObject obj = null;

            for (int i = 0; i < paragraph.ChildObjects.Count; i++)
            {
                obj = paragraph.ChildObjects[i];
                if (obj.DocumentObjectType == DocumentObjectType.TextRange)
                {
                    TextRange textRange = obj as TextRange;

                    if (textRange.Text == "中国——东盟自贸区框架")
                    {
                        //为添加脚注的字符串设置加粗格式
                        textRange.CharacterFormat.Bold = true;
                        //插入脚注
                        paragraph.ChildObjects.Insert(i + 1, footnote);
                        break;
                    }
                }
            }

            //添加脚注内容被设置字体格式
            TextRange text = footnote.TextBody.AddParagraph().AppendText("2002年11月4日,朱镕基总理和东盟10国领导人共同签署了《中国-东盟全面经济合作框架协议》,这标志着中国与东盟的经贸合作进入了一个新的历史阶段。");
            text.CharacterFormat.FontName = "Arial Black";
            text.CharacterFormat.FontSize = 9;
            text.CharacterFormat.TextColor = Color.DarkGray;
            footnote.MarkerCharacterFormat.FontName = "Calibri";
            footnote.MarkerCharacterFormat.FontSize = 12;
            footnote.MarkerCharacterFormat.Bold = true;
            footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

            //获取第5段落
            Paragraph paragraph3 = document.Sections[0].Paragraphs[4];

            //添加尾注并设置尾注和格式
            Footnote endnote = paragraph3.AppendFootnote(FootnoteType.Endnote);

            TextRange text2 = endnote.TextBody.AddParagraph().AppendText("党的十七大报告明确指出:“坚持对外开放的基本国策,把‘引进来’和‘走出去’更好地结合起来,扩大开放领域,优化开放结构,提高开放质量,完善内外联动,互利共赢、安全高效的开放型经济体系,形成经济全球化条件下参与国际经济合作和竞争的新优势。");
            text2.CharacterFormat.FontName = "Arial Black";
            text2.CharacterFormat.FontSize = 9;
            text2.CharacterFormat.TextColor = Color.Black;
            endnote.MarkerCharacterFormat.FontName = "Calibri";
            endnote.MarkerCharacterFormat.FontSize = 12;
            endnote.MarkerCharacterFormat.Bold = false;
            endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

            //保存并打开文档
            document.SaveToFile("添加脚注尾注.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("添加脚注尾注.docx");
        }
    }
}

测试结果:
C# /VB.NET添加、读取Word脚注/尾注

【VB.NET】

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

Namespace InsertFootnote_Doc

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim document As Document = New Document()
            document.LoadFromFile("sample.docx", FileFormat.Docx2010)
            Dim paragraph As Paragraph = document.Sections(0).Paragraphs(2)
            Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)
            Dim obj As DocumentObject = Nothing
            For i As Integer = 0 To paragraph.ChildObjects.Count - 1
                obj = paragraph.ChildObjects(i)
                If obj.DocumentObjectType = DocumentObjectType.TextRange Then
                    Dim textRange As TextRange = TryCast(obj, TextRange)
                    If textRange.Text = "中国——东盟自贸区框架" Then
                        textRange.CharacterFormat.Bold = True
                        paragraph.ChildObjects.Insert(i + 1, footnote)
                        Exit For
                    End If
                End If
            Next

            Dim text As TextRange = footnote.TextBody.AddParagraph().AppendText("2002年11月4日,朱镕基总理和东盟10国领导人共同签署了《中国-东盟全面经济合作框架协议》,这标志着中国与东盟的经贸合作进入了一个新的历史阶段。")
            text.CharacterFormat.FontName = "Arial Black"
            text.CharacterFormat.FontSize = 9
            text.CharacterFormat.TextColor = Color.DarkGray
            footnote.MarkerCharacterFormat.FontName = "Calibri"
            footnote.MarkerCharacterFormat.FontSize = 12
            footnote.MarkerCharacterFormat.Bold = True
            footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen
            Dim paragraph3 As Paragraph = document.Sections(0).Paragraphs(4)
            Dim endnote As Footnote = paragraph3.AppendFootnote(FootnoteType.Endnote)
            Dim text2 As TextRange = endnote.TextBody.AddParagraph().AppendText("党的十七大报告明确指出:“坚持对外开放的基本国策,把‘引进来’和‘走出去’更好地结合起来,扩大开放领域,优化开放结构,提高开放质量,完善内外联动,互利共赢、安全高效的开放型经济体系,形成经济全球化条件下参与国际经济合作和竞争的新优势。")
            text2.CharacterFormat.FontName = "Arial Black"
            text2.CharacterFormat.FontSize = 9
            text2.CharacterFormat.TextColor = Color.Black
            endnote.MarkerCharacterFormat.FontName = "Calibri"
            endnote.MarkerCharacterFormat.FontSize = 12
            endnote.MarkerCharacterFormat.Bold = False
            endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen
            document.SaveToFile("添加脚注尾注.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("添加脚注尾注.docx")
        End Sub
    End Class
End Namespace

第三步 读取脚注尾注

【C#】

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;

namespace ReadFootnote_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document类对象,加载需要测试的文档
            Document document = new Document();
            document.LoadFromFile("添加脚注尾注.docx");
            //获取文档第一个section
            Section section = document.Sections[0];

            //实例化StringBuilder类 
            StringBuilder sb = new StringBuilder();

            //遍历文档中所有段落
            foreach (Paragraph paragraph in section.Paragraphs)
            {
                for (int i = 0, cnt = paragraph.ChildObjects.Count; i < cnt; i++)
                {
                    ParagraphBase pBase = paragraph.ChildObjects[i] as ParagraphBase;
                    if (pBase is Footnote)
                    {
                        //若需要读取尾注,将此处FootnoteType.Footnote改成 FootnoteType.Endnote即可
                        if ((pBase as Footnote).FootnoteType == FootnoteType.Footnote)
                        {
                            foreach (Paragraph footPara in (pBase as Footnote).TextBody.Paragraphs)
                            {
                                sb.Append(footPara.Text);
                            }
                        }
                    }
                }
            }
            //将读取内容写入文本并保存
            File.WriteAllText("FootNotes.txt", sb.ToString());
            //打开文档
            System.Diagnostics.Process.Start("FootNotes.txt");
        }
    }
}

读取脚注:
C# /VB.NET添加、读取Word脚注/尾注

读取尾注:

C# /VB.NET添加、读取Word脚注/尾注

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.IO
Imports System.Text

Namespace ReadFootnote_Doc

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim document As Document = New Document()
            document.LoadFromFile("添加脚注尾注.docx")
            Dim section As Section = document.Sections(0)
            Dim sb As StringBuilder = New StringBuilder()
            For Each paragraph As Paragraph In section.Paragraphs
                While i < cnt
                    Dim pBase As ParagraphBase = TryCast(paragraph.ChildObjects(i), ParagraphBase)
                    If TypeOf pBase Is Footnote Then
                        If(TryCast(pBase, Footnote)).FootnoteType = FootnoteType.Footnote Then
                            For Each footPara As Paragraph In(TryCast(pBase, Footnote)).TextBody.Paragraphs
                                sb.Append(footPara.Text)
                            Next
                        End If
                    End If

                    i += 1
                End While
            Next

            File.WriteAllText("FootNotes.txt", sb.ToString())
            System.Diagnostics.Process.Start("FootNotes.txt")
        End Sub
    End Class
End Namespace

《本文完》

推荐阅读:
  1. oracle 插入
  2. android 截取验证码的两种实现方式

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

插入

上一篇:oracle next_day,add_month函数

下一篇:微信有哪些分销模式可以发展?

相关阅读

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

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