Issue with replace text with string or html

I want to replace a string in my paragraph with another string or html
Iusse is… the string I want to replace is part of multiple runs.

When i try to replace

  1. When string to be replaced is in single run, it replaces properly.
  2. When string to be replaced is part of multiple runs, it doesnt replace.

Following is the code…

                                                     FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
                                                     findReplaceOptions.setReplacingCallback(new FindAndInsertString((String)row.getAttribute("VariableCode"),(String)row.getAttribute("VariableType")));
                                                     findReplaceOptions.setPreserveMetaCharacters(true);
                                                     if(row.getAttribute("VariableValue")!=null){
                                                         doc.getRange().replace(var,(String)row.getAttribute("VariableValue") , findReplaceOptions);
                                                         
                                                     }

Following is the logic for IReplacingHandler

package oracle.apps.okc.aspose.util;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.IReplacingCallback;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.ReplaceAction;
import com.aspose.words.ReplacingArgs;
import com.aspose.words.Run;

import java.util.ArrayList;

public class FindAndInsertString implements IReplacingCallback {
private String varCode;
private String varType;
FindAndInsertString(String varCode, String varType){
this.varCode=varCode;
this.varType=varType;
}
public int replacing(ReplacingArgs e) throws Exception {
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.getMatchNode();
Node pointerNode = currentNode;
Document document = (Document) currentNode.getDocument();
DocumentBuilder builder = new DocumentBuilder(document);

    if(e.getMatchOffset()>0){
        currentNode = splitRun((Run)currentNode, e.getMatchOffset());
    }
    
    ArrayList<Node> runs = new ArrayList<Node>();
    
    int remainingLength =e.getMatch().toString().length();
    
    while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength))
            {
               runs.add(currentNode);
               
                remainingLength = remainingLength - currentNode.getText().length();

                // Select the next Run node. 
                // Have to loop because there could be other nodes such as BookmarkStart etc.
                do
                {
                    currentNode = currentNode.getNextSibling();
                }
                while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
    }
    
   /* // Split the last run that contains the match if there is any text left.
    if ((currentNode != null) && (remainingLength > 0))
    {
        splitRun((Run)currentNode, remainingLength);
        runs.add(currentNode);
    } */              
    builder.moveTo(pointerNode);
    
   
    builder.startBookmark("VAR_"+varType+"_"+varCode);
    //builder.insertDocument(insertDoc, ImportFormatMode.USE_DESTINATION_STYLES); 
    builder.write(e.getReplacement());
    builder.endBookmark("VAR_"+varType+"_"+varCode);
    for (Node run : runs){
        ((Run)run).setText("");
    }           
    
    
    
    //currentNode.remove();
    //Signal to the replace engine to do nothing because we have already done all what we wanted.
    return ReplaceAction.SKIP;
}

private Run splitRun(Run run, int position)
{
    Run afterRun = (Run)run.deepClone(true);
    afterRun.setText(run.getText().substring(position)); 
    run.setText(run.getText().substring(0, position));
    run.getParentNode().insertAfter(afterRun, run);
    return afterRun;
}

}

Note: for html insertion… used builder.insert html instead of builder.write

@mvpraveen88,

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

  • Your simplified input Word document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Your expected DOCX Word document showing the correct output. You can create expected document by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

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.