Documents for Word, .NET Edition
Features / Links / Bookmark
In This Topic
    In This Topic

    A bookmark marks a specific location in a document for referencing and assigns a name to that location so that the user can easily navigate to that particular location. GcWord provides you Bookmark class which represents a bookmark range in the content. Using GcWord, you can add bookmarks in a document with Add method of the BookmarkCollection class and delete them using Delete method of the Bookmark class.

    Bookmark in a Word document

    Add Bookmark

    To add a bookmark:

    1. Define a bookmark.
    2. Add a paragraph to be marked by a bookmark.
    3. Apply bookmark to the paragraph by adding a bookmark to the bookmark collection using Add method of the BookmarkCollection class.
    4. Add a hyperlink that navigates to the bookmark location on clicking, using the Add method of the HyperlinkCollection class.
      Copy Code
      var section = doc.Body.Sections.First;
      // Add the first paragraph          
      var p = section.GetRange().Paragraphs.Add("A bookmark marks a specific location" +
          " in a document for referencing and assigns a name to that location so that" +
          " the user can easily navigate to that particular location.");
      //Add a paragraph to the section
      for (var pr = 1; pr <= 30; pr++)
          section.GetRange().Paragraphs.Add("Section1: Test Paragraph");
      //Define bookmark name
      var bmk = "Bookmark1";
      // Add a paragraph
      var pb = section.GetRange().Paragraphs.Add($"{bmk} points here.");
      //Mark the paragraph with a bookmark
      //Create hyperlink text to jump to the bookmark location(paragraph)
      p.GetRange().Runs.Add("Jump to");
      p.GetRange().Hyperlinks.Add(bmk, $"{bmk}");
      p.GetRange().Runs.Add("at the end of the document.");
      //Save the document
    Back to Top

    Modify Bookmark

    To modify a bookmark:

    1. Access a bookmark to modify from the bookmarks collection using Bookmarks property of the RangeBase class. For example, access the first bookmark.
    2. Modify the bookmark name using Name property of the Bookmark class.
    3. Update the bookmark name in the hyperlink created for the bookmark.        
      Copy Code
      //Modify the bookamrk name
      var newBmk = "TestBookmark";
      Bookmark bmark = 
      bmark.Name = newBmk;
      //Update the bookmark name in the hyperlink created for the bookmark
      Hyperlink hyperlink_bookmark = 
      hyperlink_bookmark.Anchor = newBmk;
      hyperlink_bookmark.GetRange().Texts[0].Value = newBmk;
      //Save the document
    Back to Top

    Delete Bookmark

    To delete a bookmark, access the bookmark from the bookmark collection using Bookmarks property of the RangeBase class and delete it using the Delete method.

    Copy Code
    //Load the existing word document in GcWord instance
    //Delete bookmark
    Bookmark bmark = 
    //Delete hyperlink to bookmark
    //Save the document
    Back to Top

    For more information on how to implement bookmarks using GcWord, see GcWord sample browser.