Spire.Doc
Thursday, 14 June 2012 02:30

How to Add Footnote in Word with C#, VB.NET

Written by support iceblue

Footnote is one kind of annotations to explain one word or sentence in contents. It is often put on the bottom of one page. Because of footnote, users can learn some complicated words more clearly and the completeness can be preserved.

So, this guide will show how to add footnote in a Word document with C#/VB.NET by using Spire.Doc for .NET.

Friendly Reminder: Make Sure Spire.Doc and Visual Studio are correctly installed on system.

Step 1: Declare a new document and use document.LoadFromFile() to get Word document which you want to add footnote. Then, get the second paragraph for which the footnote will be given from the first section.

[C#]
            Document document = new Document();
            document.LoadFromFile(@"E:\work\documents\Antarctic.docx");
            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];
[VB.NET]
            Dim document As New Document()
            document.LoadFromFile("E:\work\documents\Antarctic.docx")
            Dim section As Section = document.Sections(0)
            Dim paragraph As Paragraph = section.Paragraphs(1)

Step 2: Use paragraph.AppendFootnote() method to add footnote. The parameter passed to this method is FootnotType. Then, add footnote contents by using footnote.TextBody.AddParagraph().AppendText() method. Finally, set contents format, including font style, size and color.

[C#]
            Footnote footnote=paragraph.AppendFootnote(FootnoteType.Footnote);
            TextRange text = footnote.TextBody.AddParagraph().AppendText("Check Wiki to get more information");
            text.CharacterFormat.FontName = "Arial Black";
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.TextColor = Color.SlateGray;
[VB.NET]
            Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)
            Dim text As TextRange = footnote.TextBody.AddParagraph().AppendText("Check Wiki to get more information")
            text.CharacterFormat.FontName = "Arial Black"
            text.CharacterFormat.FontSize = 12
            text.CharacterFormat.TextColor = Color.SlateGray

Step 3: As is known, there is a marker to be shown on the location where you want to add footnote. So this step is used to set marker format. Assign values for footnote.MarkerCharacterFormat.FontName/Fontsize/Bold/TextColor.

[C#]
            footnote.MarkerCharacterFormat.FontName = "Calibri";
            footnote.MarkerCharacterFormat.FontSize = 15;
            footnote.MarkerCharacterFormat.Bold = true;
            footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan;
[VB.NET]
            footnote.MarkerCharacterFormat.FontName = "Calibri"
            footnote.MarkerCharacterFormat.FontSize = 15
            footnote.MarkerCharacterFormat.Bold = True
            footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan

Step 4: Save this document by using document.SaveToFile() method. There are two parameters passed: file name string and file format. Then, launch this file for viewing.

[C#]
            document.SaveToFile("FootNote.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("FootNote.docx");
[VB.NET]
            document.SaveToFile("FootNote.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("FootNote.docx")

Insert Word Footnote

Insert Word Footnote

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET. Click to learn more

It is a little bothered to find specified text, phrase or sentence in a Word document with a large amount of contents. Therefore, the function, which enables users to find text in Word document quickly, is import and necessary. What's more, the found texts will be highlighted with a background color so that users can confirm the location more conveniently.

Spire.Doc for .NET, a professional .NET Word component, enables developers to find and highlight text in Word on their .NET applications. This guide focuses on showing the solution to find and highlight text in Word by using C#, VB.NET. Please have a look at the following screenshot, which presents the result you can get.

Find and Highlight Word Text

Download and install Spire.Doc for .NET. Load document which you want to find and highlight text. Then, invoke document.FindAllString(string, bool caseSensitive, bool wholeWord) method to find text “forming” in document. Next, save the found string in a TextSelection array. Use foreach sentence to set HighlightColor property for each selection in TextSelection array. Code as following

[C#]
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordImage
{
    class ImageinWord
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\References.docx");

            TextSelection[] text = document.FindAllString("forming", false, true);
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
            }

            document.SaveToFile("FindHighlight.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("FindHighlight.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordImage
    Friend Class ImageinWord
        Shared Sub Main(ByVal args() As String)
            'Create Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\References.docx")

            Dim text() As TextSelection = document.FindAllString("forming", False, True)
            For Each seletion As TextSelection In text
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow
            Next seletion

            document.SaveToFile("FindHighlight.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("FindHighlight.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, a stand-alone Word component, which enables users to perform a wide range of Word document processing tasks with a fast speed such as generate, read, write and modify Word document in WPF, .NET and Silverlight without MS Word automation.

Word Heading can be taken as title of each part in Word document. This guide demonstrates solution to manage these Word headings and form them as a catalogue in C# and VB.NET.

In Word document, users can set some contents, for example, phrases of summary of the following paragraphs, as headings. These Word headings can be displayed in the first page of whole document and form as catalogue after arrangement so that readers can get outline of whole document. Solution in this guide presents how to manage Word headings with numbered style in a new created document in C# and VB.NET via Spire.Doc for .NET and screenshot below shows results of managing Word headings.

Word Paragraph Headings

Heading is one of kind of styles for paragraphs and Spire.Doc for .NET provides several BuiltinStyle types for paragraphs. In this example, three BuiltinStyle types will be applied: Heading1, Heading2 and Heading3. Following, details will be presented step by step.

Firstly, initialize a Document instance and add section, paragraph for this instance. Secondly, invoke AppendText method with parameter string text and ApplyStyle(BuiltinStyle.Heading1) method of Paragraph class to add text and set heading style for new added paragraph. Then, invoke ListFromat.ApplyNumberedStyle() method of Paragraph class to set number list for it. Thirdly, add new paragraph and set Heading2 style for it as the previous step. Initialize a ListStyle instance for document with Numbered ListType. Then, initialize a ListLevel instance from ListStyle and set UsePrevLevelPattern and NumberPrefix properties for this instance. After that, invoke ListStyleCollection.Add(ListStyle) method of Document class and ListFormat.ApplyStyle method of Paragraph class with parameter string styleName to add ListStyle for Heading2. Fourthly, add a new paragraph and set BuiltinStyle as Heading3 and apply ListStyle for this paragraph as the previous step. Finally, invoke SaveToFile method of Document class with parameter string fileName and FileFormat to save this document. Refer the code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordHeading
{
    class Heading
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

            //Add Heading 1
            paragraph = section.AddParagraph();
            paragraph.AppendText(BuiltinStyle.Heading1.ToString());
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyNumberedStyle();

            //Add Heading 2
            paragraph = section.AddParagraph();
            paragraph.AppendText(BuiltinStyle.Heading2.ToString());
            paragraph.ApplyStyle(BuiltinStyle.Heading2);

            //List Style for Headings 2
            ListStyle listSty2 = new ListStyle(document, ListType.Numbered);
            foreach (ListLevel listLev in listSty2.Levels)
            {
                listLev.UsePrevLevelPattern = true;
                listLev.NumberPrefix = "1.";
            }
            listSty2.Name = "MyStyle2";
            document.ListStyles.Add(listSty2);
            paragraph.ListFormat.ApplyStyle(listSty2.Name);
            
            //Add List Style 3
            ListStyle listSty3 = new ListStyle(document, ListType.Numbered);
            foreach (ListLevel listLev in listSty3.Levels)
            {
                listLev.UsePrevLevelPattern = true;
                listLev.NumberPrefix = "1.1.";
            }
            listSty3.Name = "MyStyle3";
            document.ListStyles.Add(listSty3);

            //Add Heading 3
            for (int i = 0; i < 4; i++)
            {
                paragraph = section.AddParagraph();

                //Append Text
                paragraph.AppendText(BuiltinStyle.Heading3.ToString());

                //Apply List Style 3 for Heading 3
                paragraph.ApplyStyle(BuiltinStyle.Heading3);
                paragraph.ListFormat.ApplyStyle(listSty3.Name);
            }

            //Save and Launch
            document.SaveToFile("Word Headings.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Word Headings.docx");   
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordHeading
    Friend Class Heading
        Shared Sub Main(ByVal args() As String)
            'Create Document
            Dim document As New Document()
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = If(section.Paragraphs.Count > 0, section.Paragraphs(0), section.AddParagraph())

            'Add Heading 1
            paragraph = section.AddParagraph()
            paragraph.AppendText(BuiltinStyle.Heading1.ToString())
            paragraph.ApplyStyle(BuiltinStyle.Heading1)
            paragraph.ListFormat.ApplyNumberedStyle()

            'Add Heading 2
            paragraph = section.AddParagraph()
            paragraph.AppendText(BuiltinStyle.Heading2.ToString())
            paragraph.ApplyStyle(BuiltinStyle.Heading2)

            'List Style for Headings 2
            Dim listSty2 As New ListStyle(document, ListType.Numbered)
            For Each listLev As ListLevel In listSty2.Levels
                listLev.UsePrevLevelPattern = True
                listLev.NumberPrefix = "1."
            Next listLev
            listSty2.Name = "MyStyle2"
            document.ListStyles.Add(listSty2)
            paragraph.ListFormat.ApplyStyle(listSty2.Name)

            'Add List Style 3
            Dim listSty3 As New ListStyle(document, ListType.Numbered)
            For Each listLev As ListLevel In listSty3.Levels
                listLev.UsePrevLevelPattern = True
                listLev.NumberPrefix = "1.1."
            Next listLev
            listSty3.Name = "MyStyle3"
            document.ListStyles.Add(listSty3)

            'Add Heading 3
            For i As Integer = 0 To 3
                paragraph = section.AddParagraph()

                'Append Text
                paragraph.AppendText(BuiltinStyle.Heading3.ToString())

                'Apply List Style 3 for Heading 3
                paragraph.ApplyStyle(BuiltinStyle.Heading3)
                paragraph.ListFormat.ApplyStyle(listSty3.Name)
            Next i

            'Save and Launch
            document.SaveToFile("Word Headings.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Word Headings.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, as professional Word component, is very powerful on fast generating, loading, writing, modifying and saving Word documents in .NET, WPF, Silverlight without Word automation and any third party tools.