Java annotates the specified string of Word

This article describes how to annotate a specified string in a Word document in a Java program. Preceding text In, it mainly introduces the methods of adding comments for a paragraph, replying, editing and deleting comments. If you need to set comments for specific keywords or specified strings, you can refer to the methods in this article.

Using tool: free flame.doc for Java (free version)

Access method 1: through the official website download , and import the Spire.Doc.jar file to the java program.

Get method 2: Pass maven Warehouse installation import. May refer to Method course.


Java code example

import com.spire.doc.*;
import com.spire.doc.documents.CommentMark;
import com.spire.doc.documents.CommentMarkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.Comment;

public class AddCommentToCharacters {
    public static void main(String[] args) {
        //Load test document
        Document doc = new Document();

        //Find the specified string
        TextSelection[] selections = doc.findAllString("Thick thick film", true, false);

        //Get the paragraph of the key string
        Paragraph para = selections[0].getAsOneRange().getOwnerParagraph();
        int index = para.getChildObjects().indexOf(selections[0].getAsOneRange());

        //Add annotations ID
        CommentMark start = new CommentMark(doc);
        CommentMark end = new CommentMark(doc);

        //Add comments
        String str = "Annotate the specified string";
        Comment comment = new Comment(doc);
        para.getChildObjects().insert(index, start);
        para.getChildObjects().insert(index + 1, selections[0].getAsOneRange());
        para.getChildObjects().insert(index + 2,end);
        para.getChildObjects().insert(index + 3, comment);

        //Save document
        doc.saveToFile("String annotation.docx",FileFormat.Docx_2013);

Comment adding effect:


(end of this paper)

Tags: Java Maven

Posted on Wed, 06 Nov 2019 10:02:26 -0500 by nariman