wordpress hit counter
Bookmarks in Word document - OpenXML Developer - Blog - OpenXML Developer

Bookmarks in Word document

Blog

Samples, Demos, and Reference Articles

Bookmarks in Word document

Rate This
  • Comments 3

Author: Vijayeta Tilak of Sonata Software Limited

This article covers the basics of bookmark, including:

  • How bookmarks are represented in Open XML
  • How to programmatically iterate through bookmarks in a Word document
  • How to view the bookmarked text
  • How to create a new document based on the bookmark selected

The sample application, which uses the System.IO.Packaging API (.NET 3.0), is an ASP.NET application that is freely downloadable.  The goal of this article is to demonstrate the ease of accessing the content of WordprocessingML documents based on Open XML, and specifically to show how to work with bookmarks.

What is a bookmark?

A bookmark is used to mark a document or a specific place in a document for later retrieval. One location in a WordprocessingML document can be linked to another location in that document via a bookmark. A bookmark marks a particular place in a WordprocessingML document.

Bookmarked text in a document can prove useful as a marker or address that identifies a document or a specific place in a document.  It can further highlight a text portion which has to be translated from one language to another, as is common in the Translation Service sector.

 How is a bookmark represented in Open XML?

A selected portion of text or a location can be bookmarked. This is represented in OpenXml as shown below.

<w:bookmarkStart w:id="1" w:name="bookmarkName1" />

         <w:r > 

           <w:t>This is a book mark with name Name1.</w:t>     

     </w:r> 

        <w:bookmarkEnd w:id="1" />

The attached ASP.NET sample application

The attached ASP.NET application allows the user to iterate through bookmarks in a document, and to view the bookmarked text, using System.IO.Packaging API and ASP.NET.

The application lists out the bookmarks present in the document, both ’By Location' and ‘By Name’, and the user can view the text that was bookmarked, and formulate a new document with the selected bookmarks.

The User Interface of the ASP.Net application is shown in the figure below. 

As the UI suggests,once the document (say, sample.docx) is uploaded, the application searches for the bookmarks in the document. If bookmarks are present, the application displays all the bookmarks in the ListBoxes provided.

One Listbox contains the location being bookmarked in the document. This is to only list the bookmarks that point to a location and hold no text. The other ListBox contains the bookmarks that point to a portion of text, and these are the set of bookmarks the application deals with, to make a separate document of them.

The text is displayed in the text box provided, so that the user can decide which of the two actions should be taken, i.e. either the bookmarked text can form a separate document or the bookmarks can be deleted from the document.

The bookmarks are selected for either of the two options from the third ListBox.  In the first case, a set of bookmarked text forms a separate document, and in the second case a particular bookmark can be deleted. Here, when a bookmarked text is to be deleted, all the other bookmarked text, except the one selected for deletion are formed into a new document again.

Finally the application allows the user to download the document generated from the bookmarks selected.

 That's all Folks!  Try it out, and please let me know here or in the Forums section if you have any questions.

 

Attachment: Bookmarks_SourceCode.zip
  • I am new to xml.
    What does w:p or w:r mean?
    And, where can I find more information about these elements?
    I have watched the devcon demos from brian jones, kevin boske and others. I have a strong love now for XML in Office 2007, and I will continue to visit this bolg for more examples to improve my XML knowledge.

    Mr. Wilfred C. Ambrosina
  • Hi, Carl
    You can learn more about these elements from various asrticles posted in this site and you can also download the OpenXML specs,to learn about these elements from
    http://blogs.msdn.com/brian_jones/archive/2006/09/13/753096.aspx
  • I don't see the attached application. It would be nice if the code was available to see what you are talking about.
Page 1 of 1 (3 items)