Traversing to Character in Paragraph of Word Document using Java | Move to Paragraph | Paragraph Index | Character Index

hi there,
We are trying to insert field code to middle of the paragraph.

We are seeing that -

DocumentBuilder.moveToParagraph(int paragraphIndex, int characterIndex)

but after seeing the Parameters:
characterIndex - The index of the character inside the paragraph.
Currently can only specify 0 to move to the beginning of the paragraph or -1 to
move to the end of the paragraph.

characterIndex could not be the character index we need. So, it means

DocumentBuilder.moveToParagraph, could lead us up to the 
beginning or end of the paragraph but our requirement is
to reach to the specified character index in the paragraph.

For ex: “Hello World” is text in paragraph and suppose it is 6th paragraph, using
DocumentBuilder.moveToParagraph(5, 3), we need to reach at
“Hello World”
00000000001
01234567890

3rd index.
“lo World”.


Could you please suggest us some way (could be alternative) for the
same?


Thanks.

Hi Praneeth,


Thanks for your inquiry. We have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-10148. Your thread has also been linked to this issue and you will be notified as soon as it is supported. Sorry for the inconvenience.

Best regards,

@PraneethS,

Regarding WORDSNET-10148, you may use the following code as a workaround. Please see these sample input/output Word documents (Docs.zip (17.6 KB)). Hope, this helps:

int paragraphIndex = 0;
int characterIndex = 4;
String text = "demo";

Document doc = new Document("E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Paragraph targetPara = (Paragraph) builder.getCurrentStory().getChildNodes(NodeType.PARAGRAPH, true).get(paragraphIndex);
Node[] runs = targetPara.getChildNodes(NodeType.RUN, true).toArray();

for (int i = 0; i < runs.length; i++) {
    Run run = (Run) runs[i];
    int length = run.getText().length();

    Run currentNode = run;
    for (int x = 1; x < length; x++) {
        currentNode = SplitRun(currentNode, 1);
    }
}

if (characterIndex >= 0 && characterIndex < targetPara.getRuns().getCount()) {
    builder.moveTo(targetPara.getRuns().get(characterIndex));
    builder.getFont().setName("Verdana");
    builder.getFont().setSize(16);
    builder.getFont().setColor(Color.RED);
    builder.write(text);
} else {
    System.out.println("Incorrect character index specified");
}

doc.joinRunsWithSameFormatting();
doc.save("E:\\Temp\\20.3.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;
}

We will also inform you via this thread as soon as WORDSNET-10148 will be resolved in future.

The issues you have found earlier (filed as WORDSNET-10148) have been fixed in this Aspose.Words for .NET 21.2 update and this Aspose.Words for Java 21.2 update.