How to add hyperlink to given texts?

Hi, Support,

I have a question that how to add hyperlink to a given pure text selection.
For example, there are texts like “Please visit Microsoft Co-Orperation for more information about new products” existing in main document. I expect to find ‘Microsoft Co-Orperation’ and then locate its Start/End position , and then select them, and finally ,convert ‘Microsoft Co-Orperation’ as hyperlink with address =‘http://www.microsoft.com/products’.
How to work it out based on VB.net?

Thanks for your help!

@ducaisoft

Thanks for your inquiry. Please ZIP and attach your input and expected output documents. We will then guide you according to your requirement. Thanks for your cooperation.

Thanks for your reply.
Please refer to the attachment for your investigation.

Demo.zip (17.4 KB)

@ducaisoft

Thanks for sharing the detail. In your case, you need to implement IReplacingCallback interface, find the desired text using Range.Replace method and insert the hyperlink.

Please check the following code example. Hope this helps you.

Dim doc As Aspose.Words.Document = New Aspose.Words.Document("input.docx")
Dim opt As FindReplaceOptions = New FindReplaceOptions()
opt.ReplacingCallback = New FindAndInsertHyperlink("Microsoft Co-Orperation", "http://www.microsfot.com/")
doc.Range.Replace("Microsoft Co-Orperation", "", opt)
doc.Save("output.docx", Aspose.Words.SaveFormat.Docx)

Public Class FindAndInsertHyperlink
    Implements IReplacingCallback


    Private linktext As String
    Private link As String
    Private builder As DocumentBuilder

    Public Sub New(ByVal text As String, ByVal link As String)
        linktext = text
        Me.link = link
    End Sub

    Private Function IReplacingCallback_Replacing(e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
        Dim currentNode As Node = e.MatchNode
        If builder Is Nothing Then builder = New DocumentBuilder(CType(currentNode.Document, Document))
        If e.MatchOffset > 0 Then currentNode = SplitRun(CType(currentNode, Run), e.MatchOffset)
        Dim runs As ArrayList = New ArrayList()
        Dim remainingLength As Integer = e.Match.Value.Length

        While (remainingLength > 0) AndAlso (currentNode IsNot Nothing) AndAlso (currentNode.GetText().Length <= remainingLength)
            runs.Add(currentNode)
            remainingLength = remainingLength - currentNode.GetText().Length

            Do
                currentNode = currentNode.NextSibling
            Loop While (currentNode IsNot Nothing) AndAlso (currentNode.NodeType <> NodeType.Run)
        End While

        If (currentNode IsNot Nothing) AndAlso (remainingLength > 0) Then
            SplitRun(CType(currentNode, Run), remainingLength)
            runs.Add(currentNode)
        End If

        Dim run As Run = CType(runs(0), Run)
        builder.MoveTo(run)
        builder.Font.Color = Color.Blue
        builder.Font.Underline = Underline.Single
        builder.InsertHyperlink(linktext, link, False)
        builder.Font.ClearFormatting()

        For Each node As Run In runs
            node.Remove()
        Next

        Return ReplaceAction.Skip
    End Function

    Private Shared Function SplitRun(ByVal run As Run, ByVal position As Integer) As Run
        Dim afterRun As Run = CType(run.Clone(True), Run)
        afterRun.Text = run.Text.Substring(position)
        run.Text = run.Text.Substring(0, position)
        run.ParentNode.InsertAfter(afterRun, run)
        Return afterRun
    End Function

End Class

Thank you very much for your example.
It looks fine. Unfortunately, there may be some unknown thing that leads to failure of compiling the project.
Would you please check and fix my project why it cannot be complied successfully.
Please refer to the attachment for your check.

Thanks!
Demo.zip (44.3 KB)

@ducaisoft

Thanks for your inquiry. We have fixed the issues with shared project. Please check the attached project and let us know if you have any more queries.
FindAndInsertHyperlink.zip (11.6 KB)

Thank you very much!
It works well now.
Another question is that how to modify the Class to replace the found text as picture or new document?
Thanks again for your kind help.

@ducaisoft

Thanks for your inquiry. You can use the same class to find the text and replace it with picture and new document. We suggest you please read the following articles.
Find and Replace
Use DocumentBuilder to Insert Document Elements

You can insert the image into document using DocumentBuilder.InsertImage method and insert another document into your input document using DocumentBuilder.InsertDocument method.

Please check the following code snippet. You need to replace this code with your new code to insert image and document. Hope this helps you.

builder.Font.Color = Color.Blue
builder.Font.Underline = Underline.Single
builder.InsertHyperlink(linktext, link, False)
builder.Font.ClearFormatting()