C#/VB.NET: Insert Footnotes and Endnotes in Word Documents

Footnotes and endnotes are short notes that can be used to provide explanations, comments or references to certain words or sentences in a document. Footnotes usually appear at the bottom of the page containing their reference numbers, while endnotes appear at the end of the document or section. If you are writing an academic paper in Word, inserting footnotes or endnotes may be essential. This article will demonstrate how to insert footnotes and endnotes in Word documents in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert a Footnote in Word in C# and VB.NET

A footnote consists of two parts - a footnote reference mark and the corresponding footnote text. To insert a footnote for a specific text, you need to search for the text and get the paragraph where the text is located, after that add a footnote to the paragraph, then insert the footnote reference mark after the found text and set the footnote text. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Search for a specific text in the document using Document.FindString() method and get the found text as a single text range using TextSelection.GetAsOneRange() method.
  • Access the owner paragraph of the text range through TextRange.OwnerParagraph property and get the index of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
  • Add a footnote to the paragraph using Paragraph.AppendFootnote(FootnoteType.Footnote) method.
  • Insert the footnote reference mark after the text range using Paragraph.ChildObjects.Insert() method.
  • Set the footnote text using Footnote.TextBody.AddParagraph().AppendText() method.
  • Set formatting such as font name, font size and text color for the footnote text and reference mark.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertFootnote
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"Sample.docx");

            //Find a specific text in the document
            TextSelection selection = document.FindString("Spire.Doc for .NET", false, true);
            //Get the found text as a single text range
            TextRange textRange = selection.GetAsOneRange();
            //Get the owner paragraph of the text range
            Paragraph paragraph = textRange.OwnerParagraph;
            //Get the index of the text range in the paragraph
            int index = paragraph.ChildObjects.IndexOf(textRange);

            //Add a footnote to the paragraph
            Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
            //Insert the footnote reference mark after the text range
            paragraph.ChildObjects.Insert(index + 1, footnote);
            //Set the footnote text
            textRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD.");

            //Set format for the footnote text
            textRange.CharacterFormat.FontName = "Arial Black";
            textRange.CharacterFormat.FontSize = 12;
            textRange.CharacterFormat.TextColor = Color.DarkGray;

            //Set format for the footnote reference mark
            footnote.MarkerCharacterFormat.FontName = "Calibri";
            footnote.MarkerCharacterFormat.FontSize = 12;
            footnote.MarkerCharacterFormat.Bold = true;
            footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

            //Save the result document
            document.SaveToFile("InsertFootnote.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Insert Footnotes and Endnotes in Word Documents

Insert an Endnote in Word in C# and VB.NET

An endnote also consists of two parts - an endnote reference mark and the corresponding endnote text. The steps to insert an endnote for a specific text are very similar to that of the above example:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Search for a specific text in the document using Document.FindString() method and get the found text as a single text range using TextSelection.GetAsOneRange() method.
  • Access the owner paragraph of the text range through TextRange.OwnerParagraph property and get the index of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
  • Add an endnote to the paragraph using Paragraph.AppendFootnote(FootnoteType.Endnote) method.
  • Insert the endnote reference mark after the text range using Paragraph.ChildObjects.Insert() method.
  • Set the endnote text using Footnote.TextBody.AddParagraph().AppendText() method.
  • Set formatting such as font name, font size and text color for the endnote text and reference mark.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertEndnote
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"Sample.docx");

            //Find a specific text in the document
            TextSelection selection = document.FindString("Microsoft Office", false, true);
            //Get the found text as a single text range
            TextRange textRange = selection.GetAsOneRange();
            //Get the owner paragraph of the text range
            Paragraph paragraph = textRange.OwnerParagraph;
            //Get the index of the text range in the paragraph
            int index = paragraph.ChildObjects.IndexOf(textRange);

            //Add an endnote to the paragraph
            Footnote endnote = paragraph.AppendFootnote(FootnoteType.Endnote);
            //Insert the endnote reference mark after the text range
            paragraph.ChildObjects.Insert(index + 1, endnote);
            //Set the endnote text
            textRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft.");

            //Set format for the endnote text
            textRange.CharacterFormat.FontName = "Arial Black";
            textRange.CharacterFormat.FontSize = 12;
            textRange.CharacterFormat.TextColor = Color.DarkGray;

            //Set format for the endnote reference mark
            endnote.MarkerCharacterFormat.FontName = "Calibri";
            endnote.MarkerCharacterFormat.FontSize = 12;
            endnote.MarkerCharacterFormat.Bold = true;
            endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

            //Save the result document
            document.SaveToFile("InsertEndnote.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Insert Footnotes and Endnotes in Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.