DocumentBuilderInsertHyperlink Method

Inserts a hyperlink into the document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public Field InsertHyperlink(
	string displayText,
	string urlOrBookmark,
	bool isBookmark
)

Parameters

displayText
Type: SystemString
Text of the link to be displayed in the document.
urlOrBookmark
Type: SystemString
Link destination. Can be a url or a name of a bookmark inside the document. This method always adds apostrophes at the beginning and end of the url.
isBookmark
Type: SystemBoolean
True if the previous parameter is a name of a bookmark inside the document; false is the previous parameter is a URL.

Return Value

Type: Field
A Field object that represents the inserted field.
Remarks

Note that you need to specify font formatting for the hyperlink display text explicitly using the Font property.

This methods internally calls InsertField(String) to insert an MS Word HYPERLINK field into the document.

Examples
Inserts a hyperlink into a document using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Please make sure to visit ");

// Specify font formatting for the hyperlink
builder.Font.Color = Color.Blue;
builder.Font.Underline = Underline.Single;
// Insert the link.
builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false);

// Revert to default formatting
builder.Font.ClearFormatting();

builder.Write(" for more information.");

doc.Save(ArtifactsDir + "DocumentBuilder.InsertHyperlink.doc");
Examples
Inserts a hyperlink referencing local bookmark.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark("Bookmark1");
builder.Write("Bookmarked text.");
builder.EndBookmark("Bookmark1");

builder.Writeln("Some other text");

// Specify font formatting for the hyperlink
builder.Font.Color = Color.Blue;
builder.Font.Underline = Underline.Single;

// Insert hyperlink
// Switch \o is used to provide hyperlink tip text
builder.InsertHyperlink("Hyperlink Text", @"Bookmark1"" \o ""Hyperlink Tip", true);

// Clear hyperlink formatting
builder.Font.ClearFormatting();

doc.Save(ArtifactsDir + "DocumentBuilder.InsertHyperlinkToLocalBookmark.doc");
Examples
Shows how to use temporarily save and restore character formatting when building a document with DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Set up font formatting and write text that goes before the hyperlink
builder.Font.Name = "Arial";
builder.Font.Size = 24;
builder.Font.Bold = true;
builder.Write("To go to an important location, click ");

// Save the font formatting so we use different formatting for hyperlink and restore old formatting later
builder.PushFont();

// Set new font formatting for the hyperlink and insert the hyperlink
// The "Hyperlink" style is a Microsoft Word built-in style so we don't have to worry to 
// create it, it will be created automatically if it does not yet exist in the document
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
builder.InsertHyperlink("here", "http://www.google.com", false);

// Restore the formatting that was before the hyperlink.
builder.PopFont();

builder.Writeln(". We hope you enjoyed the example.");

doc.Save(ArtifactsDir + "DocumentBuilder.PushPopFont.doc");
See Also