ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
// This is the run node that contains the found text. Note that the run might contain other
// text apart from the URL. All the complexity below is just to handle that. I don't think there
// is a simpler way at the moment.
Run run = (Run)e.MatchNode;
Paragraph para = run.ParentParagraph;
string url = e.Match.Value;
// We are using \xbf (inverted question mark) symbol for temporary purposes.
// Any symbol will do that is non-special and is guaranteed not to be presented in the document.
// The purpose is to split the matched run into two and insert a hyperlink field between them.
para.Range.Replace(url, "\xbf", true, true);
Run subRun = (Run)run.Clone(false);
int pos = run.Text.IndexOf("\xbf");
subRun.Text = subRun.Text.Substring(0, pos);
run.Text = run.Text.Substring(pos + 1, run.Text.Length - pos - 1);
para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), subRun);
this.mBuilder.MoveTo(run);
// Specify font formatting for the hyperlink.
this.mBuilder.Font.Color = Color.Blue;
this.mBuilder.Font.Underline = Underline.Single;
// Insert the hyperlink.
this.mBuilder.InsertHyperlink(url, url, false);
// Clear hyperlink formatting.
this.mBuilder.Font.ClearFormatting();
// Let's remove run if it is empty.
if (run.Text.Equals(""))
run.Remove();
// No replace action is necessary - we have already done what we intended to do.
return ReplaceAction.Skip;
}