Range.Replace does not find the correct matched node using C#

Hi, based on the example of the IReplacingCallback Interface that insert a html text in the API help I wrote the following program:


class Program
{
static void Main(string[] args)
{
Document _document = null;
_document = new Document(“CartaCuenta Nueva.docx”);
_document.Range.Replace(new Regex(@"[(\w)*]"), new DocumentReplaceCustomTags(_document), true);
}
}

internal class DocumentReplaceCustomTags : IReplacingCallback
{
private Document _document;

public DocumentReplaceCustomTags(Document document)
{
this._document = document;
}


#region IReplacingCallback Members

public ReplaceAction Replacing(ReplacingArgs args)
{
string tagName;

tagName = args.Match.Value.Substring(1, args.Match.Value.Length - 2);

DocumentBuilder docBuilder = new DocumentBuilder(_document);
docBuilder.MoveTo(args.MatchNode);
docBuilder.InsertField(string.Format(@“MERGEFIELD {0} * MERGEFORMAT”, tagName));
args.Replacement = string.Empty;

return ReplaceAction.Replace;
}

#endregion
}

All works well, it finds all word that match the Regex expression, the problem is with the MatchNode, always has the same node even the match word is in another place and because of that the algoritm doesn’t work. It’s there anything work?

thank you

Hi Mauricio,

Thanks for your query. We are working over your query and will update you asap.

Hi Mauricio,

Please accept my apologies for late response.

Please use the following code snippet for your requirement. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + “CartaCuenta+Nueva.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

DocumentReplaceCustomTags customer = new DocumentReplaceCustomTags();

doc.Range.Replace(new Regex(@"\[(\w)*\]"), customer, true);

foreach (Aspose.Words.Node node in customer.hashtable.Keys)

{

builder.MoveTo(node);

builder.InsertField(customer.hashtable[node].ToString());

node.Remove();

}

doc.Save(MyDir + "AsposeOut.docx");

internal class DocumentReplaceCustomTags : IReplacingCallback

{

public Hashtable hashtable = new Hashtable();

public ReplaceAction Replacing(ReplacingArgs args)

{

string tagName;

tagName = args.Match.Value.Substring(1, args.Match.Value.Length - 2);

args.Replacement = args.Replacement + "Replace";

hashtable.Add(args.MatchNode, string.Format(@"MERGEFIELD {0} \* MERGEFORMAT", tagName));

return ReplaceAction.Replace;

}

}

Thank you Tahir, with your code it works. I just want to ask one more thing, what was the problem of the original code?, I ask you because maybe a need to do some code support and don’t make the same mistakes.

Hi Mauricio,

Thanks for your query. In your code you are inserting the mail merge field with same name as matched node’s text (tagName). E.g the text nombre is replaced with MERGEFIELD nombre * MERGEFORMAT”. This text is matched again and MoveTo method moves the cursor to the same position again. The mail merge filed has inserted at the same position.

You can check this behavior by setting break point at following line in your code and check the node.
docBuilder.MoveTo(args.MatchNode);

Hope this answers your query. Please let us know if you have any more queries.

Ok, I see my mistake, thank you

Hi Mauricio,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi, I am facing a new problem replacing words. As the previous replies shows I follow the Tahir code and recommendation, but doing some test I see that the component has problems with the replacement. With one document the algorithm works fine but with other document didn’t work.


Please this is urgent because this system its going to production very soon. I have a little program of example, if you want to see it please mail me Tahir or any person at ASPOSE that could help me. I put the two documents as Attachments.

Thank you

Hi Mauricio,

Thanks for your query. I have tested the scenario and have not found any issue with output documents while using latest version of Aspose.Words for .NET. Please use the latest version of Aspose.Words for .NET. I have attached the output documents with this post. Please let us know if you have any more queries.

Hi Tahir, first of all thank you for your quick response.


The problem is still there, it is not easy to see but if you look the merge field “Eps” it goes a line up, and the “Examen” merge field it delete the word "EXAMEN: ". The merge field “NombreUsuario” delete the word “Señor” and the merge field “FechaExamen” delete a blank space.

Also I notice that the final document its different in the Word Art. I attached an image that shows de difference.

I hope this help to locate the problem.

Hi Mauricio,

Thanks for your query. Please use the following modified code for your requirements. Hope this helps you. Please let us know if you have any more queries.

internal class DocumentReplaceCustomTags : IReplacingCallback

{

public Hashtable hashtable = new Hashtable();

public ReplaceAction Replacing(ReplacingArgs args)

{

string tagName;

tagName = args.Match.Value.Substring(1, args.Match.Value.Length - 2);

Node currentNode = args.MatchNode;

args.Replacement = args.Replacement + "_Replace";

hashtable.Add(args.MatchNode, string.Format(@"MERGEFIELD {0} \* MERGEFORMAT", tagName));

return ReplaceAction.Replace;

}

}

Document doc = new Document(MyDir + "hospital.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

DocumentReplaceCustomTags customer = new DocumentReplaceCustomTags();

doc.Range.Replace(new Regex(@"\[(\w)*\]"), customer, true);

foreach (Aspose.Words.Node node in customer.hashtable.Keys)

{

((Run)node).Text = node.Range.Text.Replace("_Replace", "");

Run run = new Run(doc, "");

node.ParentNode.InsertAfter(run, node);

builder.MoveTo(run);

builder.InsertField(customer.hashtable[node].ToString());

}

doc.Save(MyDir + "AsposeOut.docx");

The problem was resolved.


Tahir than you very much.
Hi Mauricio,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.