wordpress hit counter
Bookmarks in Word document - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Bookmarks in Word document

Bookmarks in Word document

Rate This
  • Comments 4

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: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
Page 1 of 1 (4 items)