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.
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.
public partial class MainPage : UserControl
private SaveFileDialog saveFileDialog = new SaveFileDialog();
this.saveFileDialog.Filter = “Word Document(*.docx)|*.docx”;
private void button1_Click(object sender, RoutedEventArgs e)
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);
Section section = document.Sections;
Paragraph paragraph = section.Paragraphs;
Paragraph paragraph2 = section.Paragraphs;
bool? result = this.saveFileDialog.ShowDialog();
if (result.HasValue && result.Value)
using (Stream stream = this.saveFileDialog.OpenFile())