How to Insert Bookmark in Word with Silverlight

Many people would like to insert a papered flake in one book to record where they have red. This papered flake is called bookmark. Besides actual bookmark, there are another two kinds of bookmark. One is electronic bookmark, which exists in browser. Users can collect some useful or important webpages in bookmark of browser for visiting next time conveniently. The other one is bookmark in Word (topic of this post), which is used to help users fix specified location quickly or quote specified text in document. In this post, I will introduce one method about how to insert bookmark in Word with Silverlight.

Word bookmark can be set for a word, a sentence or a paragraph. Before inserting bookmark, we need to fix location. For example, if I want to insert a bookmark for a word in paragraph 4, I should select this word firstly and then insert bookmark. Also, each bookmark has a name. Generally speaking, the name is related to contents in bookmark for learning bookmark clearly.

In this example, I prepare a Word document and will set a bookmark for one paragraph in this document. Also, the Silverlight Word component, Spire.Doc for Silverlight is used so that I have added its dll file as reference in project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as WordBookmark.xaml. Double click it to design UserControl. Add an image to set as its background. Then, add a label in it. Change the label content as topic I want to talk about and set font style, color for contents. Finally, add a button to run.

Step 2. Declare SaveFileDialog

Declare a SaveFileDialog for saving document. Then, set a filter for this dialog. Because it is Word document, so the filter format is set as .docx.

Step 3. Load Document

Right click project name to add existing item (Word document). After adding, click this item to change its Build Action as embedded resource. Then, double click run button to write code.

Declare a document. Use foreach sentence to get document name in assembly. If the name is the same with embedded resource name, load this document.

Step 4. Insert Bookmark

Get section in this document and get paragraph in this section. Use paragraph.AppendBookmarkStart() method to set beginning location of bookmark. The parameter passed to method is bookmark name. Then get another paragraph in section and use paragraph.AppendBookmarkEnd() method to set ending location of bookmark.

Step 5. Save document

Judge if the SaveFileDialog which I declare in the first step can pop up. If the result is true, use document.SaveToStream() method to save document with SaveFileDialog.

Full Extract.xaml

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.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Reflection;

using System.IO;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace BookmarkinWord

{

    public partial class MainPage : UserControl

    {

        //Declare SaveFileDialog

        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);

                    }

                }

            }

 

            //Add Bookmark

            Section section = document.Sections[0];

            Paragraph paragraph = section.Paragraphs[2];

            paragraph.AppendBookmarkStart(“NZ Intro”);

            Paragraph paragraph2 = section.Paragraphs[3];

            paragraph2.AppendBookmarkEnd(“NZ Intro”);

 

            //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 Spire.Doc for Silverlight

Advertisements

2 thoughts on “How to Insert Bookmark in Word with Silverlight

  1. View Web Page says:

    It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog! I guess for now i’ll settle for bookmarking
    and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this site with my Facebook
    group. Chat soon!

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