请问怎么给某个段落的 文本 添加批注

请问怎么给某个段落的 文本 添加批注,例如:

@ouchli 如果您想为段落或文档中的特定文本添加注释,可以使用以下代码:

Document doc = new Document("C:\\Temp\\in.docx");

String word = "test";

// Use Range.replace method to make each searched word a separate Run node.
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
doc.getRange().replace(word, "$0", opt);

// Get all runs
Iterable<Run> runs = doc.getChildNodes(NodeType.RUN, true);

for (Run run : runs)
{
    // process the runs with text that matches the searched word.
    if (run.getText().equals(word))
    {
        // Crete a comment
        Comment comment = new Comment(doc, "James Bond", "007", new Date());
        comment.getParagraphs().add(new Paragraph(doc));
        comment.getFirstParagraph().appendChild(new Run(doc, "Comment text."));
        // Wrap the Run with CommentRangeStart and CommentRangeEnd
        run.getParentNode().insertBefore(new CommentRangeStart(doc, comment.getId()), run);
        run.getParentNode().insertAfter(new CommentRangeEnd(doc, comment.getId()), run);
        // Add a comment.
        run.getParentNode().insertAfter(comment, run);
    }
}

doc.save("C:\\Temp\\out.docx");

您好,您给的示例代码没有达到我的预期,
假设我给的参数有:section索引、body索引、paragraph索引,和指定文本在段落的起始偏移量和结束偏移量
加批注,
假如图中的批注参数为:section索引=0,body索引=0, paragraph索引=1, 指定文本的偏移量:2和3

@ouchli MS Word 文档中的文本表示为 Run节点,每个 Run可以包含任意数量的字符,因此没有直接的方法通过索引将文本包装到注释范围中。 首先需要将文本拆分为 Run,以便指定索引上的文本表示为单独的 Run 节点。 请参阅我们的文档以了解有关 Aspose.Words 文档对象模型的更多信息:
https://docs.aspose.com/words/java/aspose-words-document-object-model/

For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
// Get the paragraph where comment should be inserted.
// For demonstration purposes simply use the first paragraph.
Paragraph para = doc.getFirstSection().getBody().getFirstParagraph();
int commentRangeStart = 10;
int commentRangeLength = 5;
// get the first Run that includes the desired comment range.
int currentIndex = 0;
int offset = commentRangeStart;
Node currentNode = null;
for (Run r : para.getRuns())
{
    currentIndex += r.getText().length();
    if (currentIndex >= commentRangeStart)
    {
        currentNode = r;
        break;
    }
    offset -= r.getText().length();
}
// Split run if required and get other runs that are inside the desired comment range.
if (currentNode != null)
{
    if (offset > 0)
        currentNode = splitRun((Run)currentNode, offset);

    // This array is used to store all runs that are inside the desired comment range.
    ArrayList<Run> runs = new ArrayList<Run>();
    int remainingLength = commentRangeLength;
    while (remainingLength > 0 &&
            currentNode != null &&
            currentNode.getText().length() <= remainingLength)
    {
        runs.add((Run)currentNode);
        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((Run)currentNode);
    }

    // Now wrap the resulting Run nodes into the comment:
    Comment comment = new Comment(doc, "James Bond", "007", new Date());
    comment.getParagraphs().add(new Paragraph(doc));
    comment.getFirstParagraph().appendChild(new Run(doc, "Comment text."));
    // Wrap the Run with CommentRangeStart and CommentRangeEnd
    Node first = runs.get(0);
    Node last = runs.get(runs.size() - 1);
    first.getParentNode().insertBefore(new CommentRangeStart(doc, comment.getId()), first);
    last.getParentNode().insertAfter(new CommentRangeEnd(doc, comment.getId()), last);
    // Add a comment.
    last.getParentNode().insertAfter(comment, last);
}

doc.save("C:\\Temp\\out.docx");
private static Run splitRun(Run run, int position)
{
    Run afterRun = (Run)run.deepClone(true);
    run.getParentNode().insertAfter(afterRun, run);
    afterRun.setText(run.getText().substring(position));
    run.setText(run.getText().substring(0, position));
    return afterRun;
}