Hyperlinks and Bookmarks in Word Documents
- 5 minutes to read
Hyperlinks
The HyperlinkCollection contains all document hyperlinks. Use the SubDocument.Hyperlinks property to access a specific hyperlink by its index. Call the ReadOnlyHyperlinkCollection.Get method to retrieve the hyperlink related to the specified range.
Create a Hyperlink
Call the HyperlinkCollection.Create method to create a new Hyperlink.
The code snippet below finds a specific range in the document and converts it to a hyperlink with the specified URI and tooltip:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using (var wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument("Documents//Document.docx");
Document document = wordProcessor.Document;
// Find a specific text string
DocumentRange[] foundRanges =
document.FindAll("DevExpress WinForms Rich Text Editor",
SearchOptions.CaseSensitive);
if (foundRanges.Length > 0)
{
// Convert the found range to a hyperlink
Hyperlink hyperlink = document.Hyperlinks.Create(foundRanges[0]);
// Specify the URI and the tooltip
hyperlink.NavigateUri =
"https://www.devexpress.com/Products/NET/Controls/WinForms/Rich_Editor/";
hyperlink.ToolTip = "WinForms Rich Text Editor";
}
}
You can use the Create method to convert static URIs to hyperlinks. Refer to the following article for a code sample:
Note
The HYPERLINK field represents hyperlinks in the RichEditDocumentServer, therefore the DocumentCapabilitiesOptions.Fields property affects the hyperlink loading process when importing the document. Set the RichEditControlCompatibility.LoadHyperlinkAsField property to false before the InitializeComponent method call to correct this behavior and allow the control to load hyperlinks when field loading is disabled.
Remove a Hyperlink
Call the HyperlinkCollection.Remove to delete the hyperlink. The HyperlinkCollection.RemoveAt method deletes the hyperlink at the specified index in the collection.
The code sample below locates and removes all hyperlinks in the first section:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using (var wordProcessor = new RichEditDocumentServer())
{
// Load a document
wordProcessor.LoadDocument("Documents//Document.docx");
Document document = wordProcessor.Document;
// Obtain all hyperlinks in the first section
var hyperlinks = document.Hyperlinks.Get(document.Sections[0].Range);
if (hyperlinks != null)
{
// Remove all hyperlinks
foreach (Hyperlink hyperlink in hyperlinks)
{
document.Hyperlinks.Remove(hyperlink);
}
}
}
Bookmarks
The BookmarkCollection contains all document bookmarks. Use the SubDocument.Bookmarks property to access a specific bookmark by its index. Call the ReadOnlyBookmarkCollection.Get method to retrieve the bookmark related to the specified range.
Create a Bookmark
The following code snippet creates a Bookmark and a hyperlink associated with this bookmark:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using (var wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument("Documents//Document.docx");
Document document = wordProcessor.Document;
// Create a bookmark at the document start
DocumentRange start = document.CreateRange(document.Range.Start, 1);
document.Bookmarks.Create(start, "Top");
// Insert the hyperlink anchored to the created bookmark
DocumentRange[] foundRanges = document.FindAll("To the Top", SearchOptions.CaseSensitive);
if (foundRanges.Length > 0)
{
Hyperlink hyperlink = document.Hyperlinks.Create(foundRanges[0]);
hyperlink.Anchor = "Top";
}
}
Note
If a new bookmark’s name matches an existing one, an InvalidOperationException exception is thrown. Use the BookmarkOptions.ConflictNameResolution property to process bookmarks with duplicate names.
Export Bookmarks to the PDF format
You can specify what bookmarks can be displayed in the Bookmarks panel when you export the document to PDF format. Set the BookmarkOptions.DisplayBookmarksInPdfNavigationPane property to one of the PdfBookmarkDisplayMode enumeration values, as shown below:
using DevExpress.XtraRichEdit;
using (var wordProcessor = new RichEditDocumentServer())
{
//...
BookmarkOptions bookmarkOptions = wordProcessor.Options.Bookmarks;
bookmarkOptions.DisplayBookmarksInPdfNavigationPane =
PdfBookmarkDisplayMode.TocBookmarks;
}
Use the DisplayUnreferencedPdfBookmarks property to determine whether to show bookmarks without references (i.e., without hyperlinks anchored to these bookmarks) in the Bookmarks navigation pane. The DisplayBookmarksInPdfNavigationPane property set PdfBookmarkDisplayMode.None controls the unreferenced bookmarks when the DisplayUnreferencedPdfBookmarks property is set to true.
Note
Bookmark visibility options (Visibility and Color) do not affect documents that you export or print from the RichEditDocumentServer. You must specify these properties in the Rich Text Editor or other word processing applications to show the bookmark.
Remove a Bookmark
Utilize the BookmarkCollection.Remove method to delete the bookmark.
The code sample below shows how to locate and remove bookmarks in the 6th paragraph:
using DevExpress.XtraRichEdit.API.Native;
using (var wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument("Documents//Document.docx");
Document document = wordProcessor.Document;
var bookmarks =
document.Bookmarks.Get(document.Paragraphs[5].Range);
if (bookmarks != null)
{
foreach (Bookmark bookmark in bookmarks)
{
document.Bookmarks.Remove(bookmark);
}
}
}