We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Autodetect Hyperlinks

I am using aspose words to take RTF from a rich textbox, insert into a word document then convert to HTML for sending in an email.

The issue I have is that in the RTF box the URL’s are auto detected and become links. This is obviously not in the RTF markup so is not converted into the eventual HTML.

Is there a way while it is in the Aspose words document to auto link all URL’s ?


Hi John,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. You can use ReplacingCallback to replace plain text with hyperlinks in your document. I created a simple code example for you:

// Open the document.

Document doc = new Document(@"Test001\in.doc");

doc.Range.Replace(new Regex("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"),

new ReplaceWithHyperlinkEvaluator(), false);

// Save the modified document.



private class ReplaceWithHyperlinkEvaluator : IReplacingCallback


public ReplaceAction Replacing(ReplacingArgs args)


// This is a Run node that contains either the beginning or the complete match.

Node currentNode = args.MatchNode;

// The first (and may be the only) run can contain text before the match,

// in this case it is necessary to split the run.

if (args.MatchOffset > 0)

currentNode = SplitRun((Run)currentNode, args.MatchOffset);

// This array is used to store all nodes of the match for further removing.

ArrayList runs = new ArrayList();

// Get url.

string url = args.Match.Value;

// Find all runs that contain parts of the match string.

int remainingLength = args.Match.Value.Length;

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.GetText().Length <= remainingLength))



remainingLength = remainingLength - currentNode.GetText().Length;

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.



currentNode = currentNode.NextSibling;


while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));


// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0))


SplitRun((Run)currentNode, remainingLength);



// Create DocumentBuilder and move its cursor to the match node.

DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);


// Insert Hyperlink

builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;

builder.InsertHyperlink(url, url, false);

// Now remove all runs in the sequence.

foreach (Run run in runs)


// Signal to the replace engine to do nothing because we have already done all what we wanted.

return ReplaceAction.Skip;



/// Splits text of the specified run into two runs.

/// Inserts the new run just after the specified run.


private static Run SplitRun(Run run, int position)


Run afterRun = (Run)run.Clone(true);

afterRun.Text = run.Text.Substring(position);

run.Text = run.Text.Substring(0, position);

run.ParentNode.InsertAfter(afterRun, run);

return afterRun;



Hope this helps. Please let me know if you need more assistance, I will be glad to help you.

Best regards.

In what namespace or dll are IReplacingCallback<o:p></o:p> and ReplacingArgs. I am currently using aspose words 9.1 for .net (though will upgrade to the latest soon)


Hi John,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

There were some breaking changes in API in Aspose.Words 9.2.0. And IReplacingCallback replaced ReplaceEvaluator. You can learn more about breaking changes in Aspose.Words 9.2.0 release notes:


I used the latest version of Aspose.Words (9.3.0) to create this code example:


Best regards,

Thanks, I have got the C# sample working however when I converted it to VB.net the Regex was now invalid? This may be beyond the scope of support however do you have any ideas why?

Actually do not worry just spotted the double escape in the regex string.

All working now thank you for your help

Hi John,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

It is perfect that you found the solution. Please let me know if you need more assistance, I will be glad to help you.

Best regards,