Hi, based on the example of the IReplacingCallback Interface that insert a html text in the API help I wrote the following program:
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.
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.
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.