How to Add Footnote in Word with Silverlight

When we read a book, we can find there are some contents which are used to explain one word or sentence in body at the end of one page. These contents are called footnote. In order to let readers know the word or sentence footnote explains, there will be a marker (e.g. number) added on upper-right corner.

In Microsoft Word, users also can add footnote. Footnote in Word is similar to in books. It is put on the end of one page and a border will be used to separate footnote contents with body. Generally speaking, in order to highlight footnote, users may set different color or font style for marker and footnote.

Then, I will share a method to add footnote in Word with Silverlight. In my example, I use a component, Spire.Doc for Silverlight to make the function to be realized more easily and quickly. So, I have added its dll file as reference in project.

The following screenshot shows the document without footnote.

STEPS

Step 1. Declare SaveFileDialog

Firstly, I need to declare a SaveFileDialog to save document. Also, I set a filter for this SaveFileDialog to confirm which kind of format the document will be saved as. I set the filter format as Word Document (*.docx).

Step 2. Load Document

Add a button in UserControl and right-click project name to add existing item (document which I want to add footnote). Then, change the item Build Action as Embedded Resource. After that, double click button to write code.

Declare document and assembly. Use foreach sentence to get name from assembly. If the name is the same as embedded resource name, load this document.

Step 3. Add Footnote

Get section and paragraph for which I want to add footnote. Then, use paragraph.AppendFootnote(footnote type) method to add footnote. Next, give footnote contents and set format (font name, size and color) for contents. Also, set format for marker.

Step 4. Save Document

Judge if the SaveFileDialog which is declared in the first step can pop up. If the result is true, save this document by using document.SaveToStream(stream, fileformat) method.

SL-Footnote.xaml.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Drawing;

using System.Windows.Shapes;

using System.IO;

using System.Reflection;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace _SL_Footnote

{

    public partial class MainPage : UserControl

    {

        private SaveFileDialog saveFileDialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFileDialog.Filter = “Word Document(*.docx)|*.docx”;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith(“New Zealand.docx”))

                {

                    using (Stream docStream = assembly.GetManifestResourceStream(name))

                    {

                        document = new Document(docStream, FileFormat.Docx);

                    }

                }

            }

 

            //Get Paragraph

            Section section = document.Sections[0];

            Paragraph paragraph = section.Paragraphs[2];

 

            //Add Footnote and Set Footnote Format

            Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);

            TextRange text = footnote.TextBody.AddParagraph().AppendText(“A Brief Conclusion of the Country: New Zealand “);

            text.CharacterFormat.FontName = “Forte”;

            text.CharacterFormat.FontSize = 11;

            text.CharacterFormat.TextColor = Color.Brown;

 

            //Set Marker Format

            footnote.MarkerCharacterFormat.FontName = “Arial Black”;

            footnote.MarkerCharacterFormat.FontSize = 13;

            footnote.MarkerCharacterFormat.Bold = true;

            footnote.MarkerCharacterFormat.TextColor = Color.DarkOrange;

 

            //Save Document

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

 

Result

 

Freely Download Silverlight Word Component

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s