Back to Devexpress

Footnotes and Endnotes in Rich Text Documents

windowsforms-401716-controls-and-libraries-rich-text-editor-rich-edit-control-document-footnotes-and-endnotes.md

latest24.9 KB
Original Source

Footnotes and Endnotes in Rich Text Documents

  • Dec 12, 2022
  • 10 minutes to read

The Rich Text Editor 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 RichEditControl supports footnotes/endnotes in printed documents and the following file formats:

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

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.

Insert a Note

In Code

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. The code samples below show how to insert new footnotes and endnotes.

View Example

Footnote

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

private void InsertFootnotes(Document document)
{
    document.LoadDocument("Documents//Grimm.docx");

    //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 InsertFootnotes(ByVal document As Document)
    document.LoadDocument("Documents//Grimm.docx")

    '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 last paragraph 
    ' in the section with a custom mark:
    Dim l As Integer = document.Sections(0).Paragraphs.Count - 1
    Dim footnoteWithCustomMarkPosition As DocumentPosition = document.CreatePosition(document.Paragraphs(l).Range.[End].ToInt() - 1)
    document.Footnotes.Insert(footnoteWithCustomMarkPosition, "º")
End Sub

Endnote

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

private void InsertEndnotes(Document document)
{
    document.LoadDocument("Documents//Grimm.docx");

    //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 InsertEndnotes(ByVal document As Document)
    document.LoadDocument("Documents//Grimm.docx")

    '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

From the User Interface

Use buttons on the References Ribbon tab to insert a footnote or endnote.

Access and Edit a Note

In Code

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 content (the SubDocument object).

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

APIDescription
SubDocument.RangeRetrieves the range of the note’s content.
SubDocument.CreateRange()Defines a document range. Use this method to modify a part of 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(Document document)
{
    document.LoadDocument("Documents//Grimm.docx");

    //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 document As Document)
  document.LoadDocument("Documents//Grimm.docx")

  '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(Document document)
{
    document.LoadDocument("Documents//Grimm.docx");

    //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 document As Document)
  document.LoadDocument("Documents//Grimm.docx")

  '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

From the User Interface

The Footnotes ribbon group on the References tab allows users to navigate to the next/previous note.

Click Show Notes to invoke the Show Notes dialog. Select the footnote or endnote area to show to the first footnote or endnote after the cursor’s position.

You can edit a note’s content as regular text. Refer to the following help article for information on how to format text: Text Formatting.

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 Rich Text Editor 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(Document document)
{
    document.LoadDocument("Documents//Grimm.docx");

    //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 document As Document)
    document.LoadDocument("Documents//Grimm.docx")

    '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

Note

The Rich Text Editor has no user interface elements to edit footnote or endnote separators.

Convert Notes

In Code

Use methods from the table below to convert notes:

MemberDescription
Note.ConvertConvert a footnote to an endnote or vice versa. A note is added to the end of the corresponding collection.
NoteCollection.ConvertConverts all footnotes to endnotes and vice versa. Notes are added to the end of the corresponding collection.
NoteCollection.Swap()Swaps footnotes with endnotes.
csharp
using DevExpress.XtraRichEdit.API.Native;

private void ConvertNotes(Document document)
{
    document.LoadDocument("FirstLook.docx");

    //Convert the third footnote to an endnote:
    document.Footnotes[2].Convert();

    //Convert all endnotes to footnotes:
    document.Endnotes.Convert();

    //Uncomment the line below to swap notes:
    //document.Endnotes.Swap();
}
vb
Imports DevExpress.XtraRichEdit.API.Native

Private Sub ConvertNotes(ByVal document As Document)
    document.LoadDocument("FirstLook.docx")

    'Convert the third footnote to an endnote:
    document.Footnotes(2).Convert()

    'Convert all endnotes to footnotes:
    document.Endnotes.Convert()

    'Uncomment the line below to swap notes:
    'document.Endnotes.Swap();
End Sub

From the User Interface

Right-click a footnote or endnote and click Convert to Endnote or Convert to Footnote to convert a note.

Invoke the Footnote and Endnote dialog dialog and click Convert to open the Convert Notes dialog. This dialog includes three conversion options:

  • all footnotes to endnotes
  • all endnotes to footnotes
  • both ways (swap footnotes and endnotes)

Change Note Appearance

In Code

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
richEditControl.LoadDocument("Document.docx");
Document document = richEditControl.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
richEditControl.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

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
richEditControl.LoadDocument("Document.docx");
Document document = richEditControl.Document
document.EndnotePosition = EndnotePosition.EndOfSection;

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

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

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
richEditControl.LoadDocument("Document.docx");
Document document = richEditControl.Document;

foreach (Section section in document.Sections)
{
    section.FootnoteOptions.ColumnCount = 3;
}
vb
richEditControl.LoadDocument("Document.docx")
    document As Document = richEditControl.Document

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

From the User Interface

The Footnotes dialog box launcher invokes the Footnote and Endnote dialog. Options in this dialog allow users to specify note location, layout, and format.

This dialog is also available from the context menu.

Remove a Note

In Code

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(Document document)
{
    document.LoadDocument("Documents//Grimm.docx");

    //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 document As Document)
document.LoadDocument("Documents//Grimm.docx")

'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

From the User Interface

Delete the note reference to remove a footnote or endnote. Right-click the note and click Go to Footnote or Go to Endnote to locate the reference.