Find Non-Breaking Spaces in Word Document and Replace with Normal Spaces | Java

Hello,

I need to replace the non breaking spaces in the entire document with normal space (" ") to do some operations and then replace those runs with non breaking spaces again.

To achieve this, I have used FindAndReplace Options and collected a list of runs of which non breaking space was replaced. And at the end, text of all the runs of that list is set to non breaking space again.
Also, an exception is set to be thrown if that list of runs contain text other than normal space (" ").

This is working fine with aspose.words library version 20.3. No exception is thrown with given input. However, it is not working with version 21.2. The list of runs contain text other than normal space after find and replace option. Please refer to screen shot.

Could you please look into the same.
Please note: It is not working from aspose library version 20.12.

The code, input file, expected result(from v20.3) and screenshot of error result (from v21.2) are attached.

Thanks.

StandAloneCode.zip (53.8 KB)

@nobilex,

We are working on your query and will get back to you soon.

Hello @awais.hafeez,

Is there any update for the same? It is somewhat on high priority at our end actually.

@nobilex,

Please check if the following code of Aspose.Words for Java is acceptable for you?

ArrayList listOfRuns = new ArrayList(); // Will hold Run nodes containing NON_BREAKING_SPACE
Document doc = new Document("C:\\Temp\\StandAloneCode\\input.docx");

// Collect such Run nodes
for (Section section : doc.getSections()) {
    Node[] runNodes = section.getBody().getChildNodes(NodeType.RUN, true).toArray();
    for (Node node : runNodes) {
        Run run = (Run) node;
        if (run.getText().contains(ControlChar.NON_BREAKING_SPACE)) {
            ArrayList<Integer> indexes = new ArrayList<Integer>();

            char[] chars = run.getText().toCharArray();
            for (int i = 0; i < chars.length; i++)
                if (chars[i] == ControlChar.NON_BREAKING_SPACE_CHAR)
                    indexes.add(i);

            Run currentNode = run;
            for (int i = 0; i < indexes.size(); i++) {
                if (i == 0)
                    currentNode = splitRun(currentNode, indexes.get(i));
                else
                    currentNode = splitRun(currentNode, (indexes.get(i) - indexes.get(i - 1)) - 1);

                currentNode = splitRun(currentNode, 1);
                listOfRuns.add(currentNode.getPreviousSibling());
            }
        }
    }
}

// Replace non breaking space with normal space
for (Run run : (Iterable<Run>) listOfRuns)
    run.setText(" ");

// Do some operation and then set non breaking spaces again
for (Run run : (Iterable<Run>) listOfRuns)
    run.setText(ControlChar.NON_BREAKING_SPACE);

doc.save("C:\\temp\\StandAloneCode\\awjava-21.9.docx");

private static 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, position));
    run.getParentNode().insertAfter(afterRun, run);
    return afterRun;
}

Hi @awais.hafeez,

The provided work around works. Thanks.

However, is there any update for version 21.2, for using the same code ,that is, using FindAndReplace option, instead of replacing existing code with the work around?

@nobilex,

Can you please translate Kotlin source code of Demo.kt file into Java and share the native simplified Java code here for further testing? We will then be able to investigate the scenario further on our end and provide you more information. Thanks for your cooperation.