Skip to Main Content
Adding more elements to a Document – Part II

Adding more elements to a Document – Part II

Learn more about Xceed Words for .NET

 

This week, we will look at 2 more elements that can be added in your documents: Bookmarks and Hyperlinks.

Bookmarks

A bookmark in a DocX file works like a bookmark that you might place in a book, it marks a place that you want to be able to find again easily. You can give each bookmark a unique name so they are easy to identify, and there is no limit to how many you can have in a given document.

 

Adding Bookmarks

Adding a Bookmark can be done by calling InsertBookmark on the Document. You can also append a bookmark to a specific paragraph by calling AppendBookmark on that Paragraph.

// Insert a bookmark in the document.
document.InsertBookmark( "Bookmark1" );

// Append a bookmark to a specific paragraph.
var p2 = document.Paragraphs[ 2 ];
p2.AppendBookmark( "Bookmark2" );

 

Removing Bookmarks

Removing an existing bookmark from a paragraph is done by calling RemoveBookmark on the Paragraph.

// Remove an existing bookmark from a paragraph
var p3 = document.Paragraphs[ 3 ];
p3.RemoveBookmark( "Bookmark3" );

 

Using Bookmarks

Bookmarks can be used to easily display or insert text at the specific position they indicate.

This can be done by calling InsertAtBookmark on either Document or Paragraph, or by calling ReplaceAtBookmark on Paragraph.

  • InsertAtBookmark: inserts the provided text before the Bookmark’s position.

  • ReplaceAtBookmark: replaces the text at the Bookmark’s position.

// Insert new text before a document’s bookmark.
document.InsertAtBookmark( "Text to insert", "Bookmark1" );

// Insert new text before a paragraph’s bookmark.
var p2 = document.Paragraphs[ 2 ];
p2.InsertAtBookmark( "Text to insert", "Bookmark2" );

// Replace a bookmark’s text
var p3 = document.Paragraphs[ 3 ];
p3.ReplaceAtBookmark( "New Text", "Bookmark3" );

 

Note: both InsertAtBookmark and ReplaceAtBookmark accept a 3rd optional parameter to specify the Formatting to apply on the text to insert or replace.

 

Retrieving Bookmarks

The list of existing Bookmarks can be retrieved by calling Bookmarks on Document, or GetBookmarks on Paragraph.

  • Bookmarks: gets the collection of bookmarks in the Document.

  • GetBookmarks: gets the list of bookmarks in the paragraph.

// Get a document’s list of bookmarks
var dBookmarks = document.Bookmarks;

// Get a paragraph’s list of bookmarks.
var p = document.Paragraphs[ 2 ];
var pBookmarks = p.GetBookmarks();

 

Hyperlinks

A hyperlink in a DocX file is one or more words that can be clicked on to jump to a new location, be it elsewhere in the current document, or to another location (such as a website’s URL to be open in a web browser). Hyperlinks are often blue and underlined, but they can have any format you choose to apply.

 

Adding Hyperlinks

Adding a Hyperlink is done by first calling AddHyperlink on the Document to add that link to the document’s hyperlink collection. Then, that hyperlink can be added to a paragraph by calling either InsertHyperlink or AppendHyperlink on Paragraph.

// Add a hyperlink to the Document’s Hyperlink collection
Hyperlink link = document.AddHyperlink( "website", new Uri( "https://xceed.com" ) );

// Add a paragraph that uses the hyperlink
document.InsertParagraph( "Visit our " ).AppendHyperlink( link ).Append( " for more information." );

 

Removing Hyperlinks

Removing an existing hyperlink from a paragraph is done by calling RemoveHyperlink on the Paragraph.

// Remove a hyperlink from a paragraph
var p3 = document.Paragraphs[ 3 ];
p3.RemoveHyperlink( 2 );

 

Retrieving Hyperlinks

The list of existing Hyperlinks can be retrieved by calling Hyperlinks on either Document or Paragraph.

// Get a document’s list of hyperlinks
var dBookmarks = document.Hyperlinks;

// Get a paragraph’s list of bookmarks.
var p = document.Paragraphs[ 2 ];
var pBookmarks = p.Hyperlinks;

 

For more information, please refer to the documentation.

Join more than 100,000 satisfied customers now!

IBM
Deloitte
Microsoft
NASA
Bank of America
JP Morgan
Apple