Back to Devexpress

Footnotes and Endnotes in Word Documents

officefileapi-401688-word-processing-document-api-word-processing-document-footnotes-and-endnotes.md

latest22.2 KB
Original Source

Footnotes and Endnotes in Word Documents

  • Oct 05, 2022
  • 9 minutes to read

The Word Processing Document API allows you to manage footnotes and endnotes. Footnotes appear at the bottom of the page and endnotes at the end of the document.

Footnotes

Endnotes

The Document.Footnotes and Document.Endnotes properties retrieve the collection of footnotes and endnotes. You can insert new notes and access, edit, and remove existing notes.

The Word Processing Document API supports footnotes/endnotes in printed documents and the following file formats:

  • DOCX
  • DOC
  • RTF
  • ODT
  • PDF (export only).

Insert a Note

Call the NoteCollection.Insert method to insert a note at a specific position. Pass the symbol used to mark a reference to insert a note with a custom mark. Code samples below show how to insert new footnotes and endnotes:

View Example

Footnote

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

static void InsertFootnotes(RichEditDocumentServer wordProcessor)
{
    wordProcessor.LoadDocument("Documents//Grimm.docx");
    Document document = wordProcessor.Document;

    //Insert a footnote at the end of the 6th paragraph:
    DocumentPosition footnotePosition = 
    document.CreatePosition(document.Paragraphs[5].Range.End.ToInt() - 1);
    document.Footnotes.Insert(footnotePosition);

    //Insert a footnote at the last paragraph 
    //in the section with a custom mark:
    int l = document.Sections[0].Paragraphs.Count - 1;
    DocumentPosition footnoteWithCustomMarkPosition =
    document.CreatePosition(document.Paragraphs[l].Range.End.ToInt() - 1);
    document.Footnotes.Insert(footnoteWithCustomMarkPosition, "\u00BA");
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Shared Sub InsertNotes(ByVal wordProcessor As RichEditDocumentServer)
    wordProcessor.LoadDocument("Documents//Grimm.docx")
    Dim document As Document = wordProcessor.Document

    'Insert a footnote at the end of the 6th paragraph:
    Dim footnotePosition As DocumentPosition = 
    document.CreatePosition(document.Paragraphs(5).Range.End.ToInt() - 1)
    document.Footnotes.Insert(footnotePosition)

    'Insert a footnote at the end of the 8th paragraph with a custom mark:
    Dim footnoteWithCustomMarkPosition As DocumentPosition = 
    document.CreatePosition(document.Paragraphs(7).Range.End.ToInt() - 1)
    document.Footnotes.Insert(footnoteWithCustomMarkPosition, ChrW(&H00BA).ToString())
End Sub

Endnote

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

static void InsertEndnotes(RichEditDocumentServer wordProcessor)
{
    wordProcessor.LoadDocument("Documents//Grimm.docx");
    Document document = wordProcessor.Document;
    //Insert an endnote at the end of the last paragraph:
    DocumentPosition endnotePosition = 
    document.CreatePosition(document.Paragraphs[document.Paragraphs.Count - 1].Range.End.ToInt() - 1);
    document.Endnotes.Insert(endnotePosition);

    //Insert an endnote at the end of 
    //the second last paragraph with a custom mark:
    DocumentPosition endnoteWithCustomMarkPosition = 
    document.CreatePosition(document.Paragraphs[document.Paragraphs.Count - 2].Range.End.ToInt() - 2);
    document.Endnotes.Insert(endnoteWithCustomMarkPosition, "\u002a");
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Shared Sub InsertNotes(ByVal wordProcessor As RichEditDocumentServer)
    wordProcessor.LoadDocument("Documents//Grimm.docx")
    Dim document As Document = wordProcessor.Document
    'Insert an endnote at the end of the last paragraph:
    Dim endnotePosition As DocumentPosition = 
    document.CreatePosition(document.Paragraphs(document.Paragraphs.Count - 1).Range.End.ToInt() - 1)
    document.Endnotes.Insert(endnotePosition)

    'Insert an endnote at the end of the second last paragraph with a custom mark:
    Dim endnoteWithCustomMarkPosition As DocumentPosition = 
    document.CreatePosition(document.Paragraphs(document.Paragraphs.Count - 2).Range.End.ToInt() - 1)
    document.Endnotes.Insert(endnoteWithCustomMarkPosition, ChrW(&H0060).ToString())
End Sub

Access and Edit a Note

Access notes by their index in the NoteCollection. An indexer returns a Note object.

Use the Note.BeginUpdate() - Note.EndUpdate() paired methods to initiate the update session and access the note’s content (the SubDocument object).

The table below lists API you can use to edit the note’s content:

APIDescription
SubDocument.RangeRetrieves the range of the note’s content.
SubDocument.CreateRange()Defines a document range. Use this method to change a note.
SubDocument.Delete()Removes all or part of the note’s content.
SubDocument.AppendText()
SubDocument.InsertText()Adds the specified text.
SubDocument.BeginUpdateCharacters()
SubDocument.EndUpdateCharacters()Initiates the update session and provides access to CharacterProperties for the specified range.

The code samples below show how to change the footnote and endnote text:

Footnote

csharp
static void EditFootNote(RichEditDocumentServer wordProcessor)
{
    wordProcessor.LoadDocument("Documents//Grimm.docx");
    Document document = wordProcessor.Document;

    //Access the first footnote's content:
    SubDocument footnote = document.Footnotes[0].BeginUpdate();

    //Exclude the reference mark and the space after it 
    //from the range to be edited:
    DocumentRange footnoteTextRange = 
    footnote.CreateRange(footnote.Range.Start.ToInt() + 2, footnote.Range.Length
        - 2);

    //Clear the range:
    footnote.Delete(footnoteTextRange);

    //Append a new text:
    footnote.AppendText("the text is removed");

    //Finalize the update:
    document.Footnotes[0].EndUpdate(footnote);
}
vb
Shared Sub EditFootNote(ByVal wordProcessor As RichEditDocumentServer)
  wordProcessor.LoadDocument("Documents//Grimm.docx")
  Dim document As Document = wordProcessor.Document

  'Access the first footnote's content:
  Dim footnote As SubDocument = document.Footnotes(0).BeginUpdate()

  'Exclude the reference mark and the space after it from the range to be edited:
  Dim footnoteTextRange As DocumentRange = 
  footnote.CreateRange(footnote.Range.Start.ToInt() + 2, footnote.Range.Length - 2)

  'Clear the range:
  footnote.Delete(footnoteTextRange)

  'Append a new text:
  footnote.AppendText("the text is removed")

  'Finalize the update:
  document.Footnotes(0).EndUpdate(footnote)
End Sub

Endnote

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

static void EditEndNote(RichEditDocumentServer wordProcessor)
{
    wordProcessor.LoadDocument("Documents//Grimm.docx");
    Document document = wordProcessor.Document;

    //Access the first endnote's content:
    SubDocument endnote = document.Endnotes[0].BeginUpdate();

    //Exclude the reference mark and the space after it 
    //from the range to be edited:
    DocumentRange endnoteTextRange = 
    endnote.CreateRange(endnote.Range.Start.ToInt() + 2, endnote.Range.Length
        - 2);

    //Access the range's character properties:
    CharacterProperties characterProperties = 
    endnote.BeginUpdateCharacters(endnoteTextRange);

    characterProperties.ForeColor = System.Drawing.Color.Red;
    characterProperties.Italic = true;

    //Finalize the character options update:
    endnote.EndUpdateCharacters(characterProperties);

    //Finalize the endnote update:
    document.Endnotes[0].EndUpdate(endnote);
}
vb
Shared Sub EditEndNote(ByVal wordProcessor As RichEditDocumentServer)
  wordProcessor.LoadDocument("Documents//Grimm.docx")
  Dim document As Document = wordProcessor.Document

  'Access the first endnote's content:
  Dim endnote As SubDocument = document.Endnotes(0).BeginUpdate()

  'Exclude the reference mark and the space after it from the range to be edited:
  Dim endnoteTextRange As DocumentRange = 
  endnote.CreateRange(endnote.Range.Start.ToInt() + 2, endnote.Range.Length - 2)

  'Access the range's character properties:
  Dim characterProperties As CharacterProperties = 
  endnote.BeginUpdateCharacters(endnoteTextRange)

  characterProperties.ForeColor = System.Drawing.Color.Red
  characterProperties.Italic = True

  'Finalize the character options update:
  endnote.EndUpdateCharacters(characterProperties)

  'Finalize the endnote update:
  document.Endnotes(0).EndUpdate(endnote)
End Sub

Edit a Note Separator

Footnotes and endnotes can have the following separators:

  • Separator - separates footnotes or endnotes from the main text;
  • Continuation Separator - separates the main text from the notes that continue from the previous page;
  • Continuation Notice (optional) - indicates that the note continues on the next page.

The Word Processing Document API allows you to modify note separators. Use API from the table below to access and edit a separator.

APIDescription
NoteCollection.HasSeparatorIndicates whether notes have the specified separator type.
NoteCollection.BeginUpdateSeparatorInitiates the update session of the specified separator type and gives access to its content (the SubDocument object). If the separator doesn’t exist, the method call creates a new separator.
SubDocument.Delete()Removes all or part of the separator content. Use the SubDocument.Range property to obtain the entire separator content. The SubDocument.CreateRange() method allows you to specify a range and delete the content partially.
SubDocument.AppendText()
SubDocument.InsertText()Adds the specified text.
SubDocument.BeginUpdateCharacters()
SubDocument.EndUpdateCharacters()Initiates the update session and provides access to CharacterProperties for the specified range.
NoteCollection.EndUpdateSeparator()Finalizes the separator update.

The code sample below shows how to change the footnote separator so it appears as follows:

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

static void EditSeparator(RichEditDocumentServer wordProcessor)
{
    wordProcessor.LoadDocument("Documents//Grimm.docx");
    Document document = wordProcessor.Document;

    //Check whether the footnotes already have a separator:
    if (document.Footnotes.HasSeparator(NoteSeparatorType.Separator))
    {
        //Initiate the update session:
        SubDocument noteSeparator = 
        document.Footnotes.BeginUpdateSeparator(NoteSeparatorType.Separator);

        //Clear the separator range:
        noteSeparator.Delete(noteSeparator.Range);

        //Append a new text:
        noteSeparator.AppendText("***");

        //Change the separator color:
        CharacterProperties characterProperties = 
        noteSeparator.BeginUpdateCharacters(noteSeparator.Range);
        characterProperties.ForeColor = System.Drawing.Color.Blue;
        noteSeparator.EndUpdateCharacters(characterProperties);

        //Finalize the update:
        document.Footnotes.EndUpdateSeparator(noteSeparator);
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Shared Sub EditSeparator(ByVal wordProcessor As RichEditDocumentServer)
    wordProcessor.LoadDocument("Documents//Grimm.docx")
    Dim document As Document = wordProcessor.Document

    'Check whether the footnotes already have a separator:
    If document.Footnotes.HasSeparator(NoteSeparatorType.Separator) Then
        'Initiate the update session:
        Dim noteSeparator As SubDocument = 
        document.Footnotes.BeginUpdateSeparator(NoteSeparatorType.Separator)

        'Clear the separator range:
        noteSeparator.Delete(noteSeparator.Range)

        'Append a new text:
      noteSeparator.AppendText("***")

        'Change the separator color:
        Dim characterProperties As CharacterProperties = 
        noteSeparator.BeginUpdateCharacters(noteSeparator.Range)
        characterProperties.ForeColor = System.Drawing.Color.Blue
        noteSeparator.EndUpdateCharacters(characterProperties)

        'Finalize the update:
        document.Footnotes.EndUpdateSeparator(noteSeparator)
    End If
End Sub

Change Note Appearance

You can specify footnote and endnote options for each document section. The Section.FootnoteOptions provides access to the footnote options, the Section.EndnoteOptions property retrieves the endnote options.

Format

The NumberingFormat, StartNumber, and RestartType properties allow you to change the note reference format. If the RestartType property is set to NoteRestartType.NewSection or NoteRestartType.NewPage , the StartNumber property value is ignored.

The code sample below shows how to change the format of notes:

csharp
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("Document.docx");
    Document document = wordProcessor.Document;

    foreach (Section section in document.Sections)
    {
        EndnoteOptions endnoteOptions = section.EndnoteOptions;
        endnoteOptions.NumberingFormat = NumberingFormat.CardinalText;
        endnoteOptions.StartNumber = 3;

        FootnoteOptions footnoteOptions = section.FootnoteOptions;
        footnoteOptions.NumberingFormat = NumberingFormat.UpperRoman;
        footnoteOptions.StartNumber = 5;
    }
}
vb
Using wordProcessor As New RichEditDocumentServer()
    wordProcessor.LoadDocument("Document.docx")
    Dim document As Document = wordProcessor.Document

For Each section As Section In document.Sections
    Dim endnoteOptions As EndnoteOptions = section.EndnoteOptions
    endnoteOptions.NumberingFormat = NumberingFormat.CardinalText
    endnoteOptions.StartNumber = 3

    Dim footnoteOptions As FootnoteOptions = section.FootnoteOptions
    footnoteOptions.NumberingFormat = NumberingFormat.UpperRoman
    footnoteOptions.StartNumber = 5
Next section
End Using

Location

Use the Document.EndnotePosition property to specify the endnote location. You can place endnotes at the end of the document or at the end of each section.

The FootnoteOptions.Position property allows you to specify one of following footnote locations:

The code sample below shows how to place endnotes at the end of each section and footnotes below text:

csharp
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("Document.docx");
    Document document = wordProcessor.Document;
    document.EndnotePosition = EndnotePosition.EndOfSection;

    foreach (Section section in document.Sections)
    {
        section.FootnoteOptions.Position = FootnotePosition.BelowText;
    }
}
vb
Using wordProcessor As New RichEditDocumentServer()
    wordProcessor.LoadDocument("Document.docx")
    Dim document As Document = wordProcessor.Document
    document.EndnotePosition = EndnotePosition.EndOfSection

    For Each section As Section In document.Sections
        section.FootnoteOptions.Position = FootnotePosition.BelowText
    Next section
End Using

Layout

Use FootnoteOptions.ColumnCount property to divide footnotes into columns.

Note

The ColumnCount property is ignored when a document is displayed in the RichEditControl. However, you can set this property in code and save its value to a file for further processing in Microsoft Word or other word processing applications.

The code sample below shows how to split footnotes into multiple columns:

csharp
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("Document.docx");
    Document document = wordProcessor.Document;

    foreach (Section section in document.Sections)
    {
    section.FootnoteOptions.ColumnCount = 3;
    }
}
vb
Using wordProcessor As New RichEditDocumentServer()
    wordProcessor.LoadDocument("Document.docx")
    Dim document As Document = wordProcessor.Document

    For Each section As Section In document.Sections
    section.FootnoteOptions.ColumnCount = 3
    Next section
End Using

Remove a Note

Use one of the following methods to delete a note:

The code sample below shows how to remove the first footnote and all endnotes with custom marks:

csharp
static void RemoveNotes(RichEditDocumentServer wordProcessor)
{
    wordProcessor.LoadDocument("Documents//Grimm.docx");
    Document document = wordProcessor.Document;

    //Remove first footnote:
    document.Footnotes.RemoveAt(0);

    //Remove all endnotes with custom marks:
    for (int i = document.Endnotes.Count - 1; i >= 0; i--)
    {
        if (document.Endnotes[i].IsCustom)
            document.Endnotes.Remove(document.Endnotes[i]);
    }
}
vb
Shared Sub RemoveNotes(ByVal wordProcessor As RichEditDocumentServer)
wordProcessor.LoadDocument("Documents//Grimm.docx")
Dim document As Document = wordProcessor.Document

'Remove first footnote:
document.Footnotes.RemoveAt(0)

'Remove all endnotes with custom marks:
For i As Integer = document.Endnotes.Count - 1 To 0 Step -1
    If document.Endnotes(i).IsCustom Then
        document.Endnotes.Remove(document.Endnotes(i))
    End If
Next i
End Sub