We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

replacing(ReplacingArgs e) mail merge happening for first tag only in the run, not for other occurrences in the same run

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;
}

@e7dt,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for Java 19.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.