Footnotes and Endnotes in Word Documents
- 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.
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:
Footnote
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");
}
Endnote
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");
}
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:
API | Description |
---|---|
SubDocument.Range | Retrieves 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
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);
}
Endnote
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);
}
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.
API | Description |
---|---|
NoteCollection.HasSeparator | Indicates whether notes have the specified separator type. |
NoteCollection.BeginUpdateSeparator | Initiates 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:
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);
}
}
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:
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;
}
}
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:
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;
}
}
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:
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument("Document.docx");
Document document = wordProcessor.Document;
foreach (Section section in document.Sections)
{
section.FootnoteOptions.ColumnCount = 3;
}
}
Remove a Note
Use one of the following methods to delete a note:
- NoteCollection.Remove() - removes the specified Note object.
- NoteCollection.RemoveAt() - removes an element at the specified index.
The code sample below shows how to remove the first footnote and all endnotes with custom marks:
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]);
}
}