Hi i am trying do insert mail merge for some tags and below is the scenario that i am facing the issue i have a Run node with multiple occurrences of same tag for example : here i have “<<Sample>>
” tag in three places in the same run
One day in May- << Sample >> swam about in a pond - << Sample >> asked every animal he met the same question << Sample >>.
Aspose works fine and mail merge happened for first occurrence only. once after the mailmerge call. not for other two fields below is how it shows :
One day in May- << Bob >> swam about in a pond - << Sample >> asked every animal he met the same question << Sample >>.
can you please help , below is my code
doc.getRange().replace(Pattern.compile("<<(.*?)>>"), “”, opts);
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertHtml());
public class ReplaceEvaluatorFindAndInsertMergefield implements IReplacingCallback {
Node currentNode = e.getMatchNode();
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run) currentNode, e.getMatchOffset());
ArrayList runs = new ArrayList();
int remainingLength = e.getMatch().group().length();
while ((remainingLength > 0) && (currentNode != null) &&
(currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();
do {
currentNode = currentNode.getNextSibling();
} while ((currentNode != null) && (currentNode.getNodeType() !=
NodeType.RUN));
}
if ((currentNode != null) && (remainingLength > 0)) {
splitRun((Run) currentNode, remainingLength);
runs.add(currentNode);
}
DocumentBuilder builder = new DocumentBuilder((Document)
e.getMatchNode().getDocument());
System.out.println("builder : " + (Document)
e.getMatchNode().getDocument());
builder.moveTo((Run) runs.get(runs.size() - 1));
builder.insertField("MERGEFIELD \"" + e.getMatch().group(1) + "\"");
for (Run run : (Iterable<Run>) runs)
run.remove();
return ReplaceAction.SKIP;
}
private Run splitRun(Run run, int position) throws Exception {
if(run.getText().length() < position)
position = run.getText().length();
Run afterRun = (Run) run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}