How to change hyperlink to mergefield

Dear All,
I’ve created an Application with a richtextbox as main editor but I could not convert a word (template) document after an User save the contents of the RTB.

  1. I have already implement the converter which replace mergefields in a opened word document to hyperlinks, so i can format and handle the hyperlinks in the RTB.
  2. When an User save the contents the RTB contents are written as .RTF file, after that i open the document with new Aspose.Word.Document([saved rtf file])

I also use the documentbuilder, but that didn’t work at all. The question is “How can i replace the hyperlinks to mergefields”
below is a part of a saved RTF file:
this is a test
the mergefields are below (wich actually are hyperlinks)
HYPERLINK "# MERGEFIELD CompanyAddress1 " Cleanwood productions
HYPERLINK "# MERGEFIELD CompanyAddress2 " Echelpoelseweg 8
HYPERLINK "# MERGEFIELD CompanyAddress3 " 7595 KA Weerselo
HYPERLINK "# MERGEFIELD Sales Person Name " Peter Conijn
HYPERLINK "# MERGEFIELD t5050.f1 " R-000180

Hello
Thanks for your request. In your case, you can use code like the following to replace text placeholders with merge fields at run-time:

Document doc = new Document("C:\\Temp\\B.docx");
Regex regex = new Regex(@"\<(?\S+)\>", RegexOptions.IgnoreCase);
// Find and replace placeholders
doc.Range.Replace(regex, new ReplacePlaceholders(), false);
doc.Save("C:\\Temp\\out.docx");

=============================================

private class ReplacePlaceholders: IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        // Get name of placeholder.
        string name = args.Match.Groups["name"].Value;
        // If name is empty string, then do nothing.
        if (string.IsNullOrEmpty(name))
            return ReplaceAction.Skip;
        // 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);
        // Create DocumentBuilder object, which will help us to insert filds.
        DocumentBuilder builder = new DocumentBuilder((Document) args.MatchNode.Document);
        // Move builder cursor to the current node.
        builder.MoveTo(currentNode);
        // Insert mergefield.
        builder.InsertField(string.Format("MERGEFIELD {0}", name), string.Format("½{0}╗", name));
        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();
        // 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))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;
            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do {
                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);
            runs.Add(currentNode);
        }
        // Now highlight all runs in the sequence.
        foreach(Run run in runs)
        run.Remove();
        // 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;
}

I use placeholders like the following
Hope this helps.
Best regards,

Hello Andrey,
Which version of Aspose.Words for .NET is needed to implement the interface IReplacingCallback.
On this moment i use the Aspose.Words 5.3.0.0.
Best regards
Hans

Hello
Thanks for your inquiry. Starting from the release of Aspose.Words for .NET 9.2, the public API has undergone some changes. To facilitate automatic porting of code from the .NET platform to the Java platform events and delegates have been replaced with callbacks and interfaces. Examples of how to modify each event to use the appropriate interface in your code are described here:
https://docs.aspose.com/words/net/aspose-words-for-net/
Best regards,