Insert a footnote in a paragraph

This article shows how to place a footnote in a paragraph using Spire.Doc for .NET, for example, after the word "Spire.Doc" in the following example:

Insert a footnote in a paragraph

Insert a footnote in a paragraph

Step 1: Load a word document, Sample.docx.

Document document1 = new Document();
document1.LoadFromFile("D:\\Sample.docx",FileFormat.Docx2010);

Step 2: Get the first paragraph of the first section of Sample.docx.

Paragraph paragraph1 = document1.Sections[0].Paragraphs[0];

Step 3: Add a footnote for paragraph1.

Footnote footnote1 = paragraph1.AppendFootnote(FootnoteType.Footnote);

Step 4: Find the word "Spire.Doc" and insert footnote1 after it.

DocumentObject obj=null;
for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
{
   obj=paragraph1.ChildObjects[i];
   if (obj.DocumentObjectType == DocumentObjectType.TextRange)
   {
      TextRange textRange = obj as TextRange;
// Find the word "Spire.Doc" in paragraph1
if (textRange.Text == "Spire.Doc")
{
//Set bold format for the word "Spire.Doc"
textRange.CharacterFormat.Bold = true;
//Insert footnote1 after the word "Spire.Doc"
paragraph1.ChildObjects.Insert(i + 1, footnote1);
break;
}
     }
 }

Step 5: Type the footnote1 text and set the text's FontName, FontSize and Color.

TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;

Step 6: Set FontName, FontSize, Bold and Color for the footnote1 number.

footnote1.MarkerCharacterFormat.FontName = "Calibri";
footnote1.MarkerCharacterFormat.FontSize = 12;
footnote1.MarkerCharacterFormat.Bold = true;
footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;

Step 7: Save Sample.docx to a new document, Footnote.docx.

document1.SaveToFile("D:\\ Footnote.docx"", FileFormat.Docx2010);

Full code:

Document document1 = new Document();
document1.LoadFromFile("D:\\Sample.docx" ,FileFormat.Docx2010);
Paragraph paragraph 1= document1.Sections[0].Paragraphs[0];
Footnote footnote1 = paragraph.AppendFootnote(FootnoteType.Footnote);
DocumentObject obj=null;
for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
{
obj=paragraph1.ChildObjects[i];
if (obj.DocumentObjectType == DocumentObjectType.TextRange)
{
TextRange textRange = obj as TextRange;
// Find the word "Spire.Doc" in paragraph1
if (textRange.Text == "Spire.Doc")
{
//Set bold format for the word "Spire.Doc"
textRange.CharacterFormat.Bold = true;
//Insert footnote1 after the word "Spire.Doc"
paragraph1.ChildObjects.Insert(i + 1, footnote1);
break;
}
}
}
TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;

footnote1.MarkerCharacterFormat.FontName = "Calibri";
footnote1.MarkerCharacterFormat.FontSize = 12;
footnote1.MarkerCharacterFormat.Bold = true;
footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;
document1.SaveToFile("Footnote.docx",FileFormat.Docx2010);

Additional Info

  • tutorial_title:
Last modified on Friday, 03 September 2021 03:58