C# /VB.NET添加、讀取Word腳註/尾註

前言

腳註和尾註是對文本的補充說明。腳註一般位於頁面的底部,可以作爲文檔某處內容的註釋;尾註一般位於文檔的末尾,列出引文 的出處等。在本示例中將介紹如何來添加或刪除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 paragraph2 = document.Sections[0].Paragraphs[4];

            //添加尾註並設置尾註和格式
            Footnote endnote = paragraph2.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 paragraph2 As Paragraph = document.Sections(0).Paragraphs(4)
            Dim endnote As Footnote = paragraph2.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

《本文完》

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章