Use InsertHyperlink to insert a hyperlink into the document. This method accepts three parameters: text of the link to be displayed in the document, link destination (URL or a name of a bookmark inside the document), and a boolean parameter that should be true if the URL is a name of a bookmark inside the document.
InsertHyperlink internally calls InsertField to insert a Microsoft Word HYPERLINK field into the document. The HYPERLINK field is created by this method in the following format:
HYPERLINK \l "bookmarkName"
HYPERLINK "http://www.mydomain.com/myurl"
The method always adds apostrophes at the beginning and end of the URL.
Note that you need to specify font formatting for the hyperlink display text explicitly using the Font property.
Example DocumentBuilderInsertHyperlink
Inserts a hyperlink into a document using DocumentBuilder.
[C#]
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(MyDir + "DocumentBuilder.InsertHyperlink Out.doc");
[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = 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(MyDir & "DocumentBuilder.InsertHyperlink Out.doc")
[Java]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Please make sure to visit ");
// Specify font formatting for the hyperlink.
builder.getFont().setColor(Color.BLUE);
builder.getFont().setUnderline(Underline.SINGLE);
// Insert the link.
builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false);
// Revert to default formatting.
builder.getFont().clearFormatting();
builder.write(" for more information.");
doc.save(getMyDir() + "DocumentBuilder.InsertHyperlink Out.doc");