Inserting Return line into bulleted list

Hi, i have a problem with inserting a return line into a bulleted list.

I have attached a zip file that contains:

ReturnTest.java My JUnit class to test the behavior
ReplaceEvaluator.java The class that replace the text in a return line char
result.docx The result i get with the code
template.docx My Word template
excepted.docx The excepted result

What i am trying to do is to replace a special word in the first list item, in this case: “{enter}” with a return line char (ControlChar.PARAGRAPH_BREAK). (I have tried with the other ControlChar breaks too)

So a return line does replace the {enter}, but a new bullet is not created, it just add a new line into the first list item paragraph.

I have tested is a Word document to be sure that it was not his default behavior, if you press ENTER inside a bulleted list item, it will create a new item with a bullet.

Can you tell me the way to achieve this, or is it a bug?
Thank you for your help!

Hi Gilles,

Thanks for your inquiry. Please use the following code snippet to achieve your requirement and read following documentation link for your kind reference.
https://docs.aspose.com/words/java/find-and-replace/

Document myDocument = new Document(MyDir + "template.docx");
myDocument.getRange().replace(Pattern.compile("\\{enter}"), new ReplaceEvaluator(), true);
myDocument.save(MyDir + "result.docx");
public class ReplaceEvaluator implements IReplacingCallback
{
    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();
        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.getMatchOffset()> 0)
            currentNode = splitRun((Run) currentNode, e.getMatchOffset());
        // This array is used to store all nodes of the match for further highlighting.
        ArrayList runs = new ArrayList();
        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().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);
        }
        DocumentBuilder builder = new DocumentBuilder((Document) currentNode.getDocument());
        builder.moveTo(currentNode);
        builder.insertBreak(BreakType.PARAGRAPH_BREAK);
        for (Run run: (Iterable) runs)
        {
            run.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) throws Exception
    {
        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;
    }
}