I’m evaluating Aspose for Java, and so far it’s been beyond my expectations. One feature I like in particular is the Mail Merge Regions capability. We have a use case where our customer wants to use this, but wants to specify their own start/end tags.
Is there any way to override the functionality to support Mail Merge Regions with custom tags? Alternatively, is there any way to programatically implement Mail Merge Regions if I know that the tag start/end is going to be OPPORTUNITY_LINEITEM_START/OPPORTUNITY_LINEITEM_END?
I tried looking at the samples, and I didn’t find anything that looked like a workaround/implementation I could use. I tried with Document.moveToMergeField(). I was thinking that I could find the start merge field node, find the end merge field node and then somehow select the area in-between. From there, i thought I could repeatedly copy the selection to “emulate” the Mail Merge Regions. It didn’t work.
well, in general aspose.words object model provides you access to all content and formatting of the document. depending on how much coding you want to do, of course you can write your own mail merge / reporting engine.
but there is no way to use the tags that you want with the mail merge engine that we have right now.
I’m not saying its easy to write your own mail merge engine for aspose.words. you’ve got to learn the object model first. go through the Aspose.Words Object Model section in wiki here https://docs.aspose.com/words/net/ for starters.
As a workaround you can preprocess the user templates before merge execution, substituting user-style region filed names with names suitable for Aspose mail merge engine. The following code illustrates this approach:
///
/// Replaces field names like 'name_Start' with names suitable for MailMerge engine like 'TableStart:name'.
///
private void ReplaceFieldName(Document doc)
{
doc.Range.Replace(new Regex(@"\w\*_START"), new ReplaceEvaluator(ReplaceFieldNameEvaluator), false);
}
private ReplaceAction ReplaceFieldNameEvaluator(object sender, ReplaceEvaluatorArgs e)
{
string fieldName = e.Match.Value;
e.Replacement = "TableStart:" + e.Match.Value.Substring(0, fieldName.Length - 6);
return (ReplaceAction.Replace);
}
The code is in c#. Please let me know if you need assistance in converting it to Java.