News Category

Java: Add Comments to Text in Word

2020-02-14 07:39:12 Written by  support iceblue
Rate this item
(0 votes)

Comments can be added to almost every element in Word, such as text, images, charts, tables, etc. This article focuses on how to add comments to selected text (phrase) in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Add a Comment to a Specific Phrase

The Paragraph.appendComment() method is used to add comments to an entire paragraph. By default, the comment mark will be placed at the end of the paragraph. To add a comment to a specific phrase, you need to place comment marks (represented by the CommentMark class) at the beginning and end of the text range. The following are the steps to add a comment to a specific phrase.

  • Create a Document object, and load the sample Word file using Document.loadFromFile() method.
  • Find the specific string from the document using Document.findAllString() method.
  • Create a comment start mark and a comment end mark, which will be placed at the beginning and end of the selected phrase respectively.
  • Create a Comment object, specifying content and author.
  • Get the oner paragraph of the selected phrase, and add the comment to the paragraph as a child object.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
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 AddCommentToSpecificText {

    public static void main(String[] args) {

        //Create a Document object
        Document doc = new Document();

        //Load the sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //Find the specific string to add comment
        TextSelection[] finds = doc.findAllString("Spire.Doc for Java", false, true);
        TextSelection specificText = finds[0];

        //Create a start mark 
        CommentMark commentmarkStart = new CommentMark(doc);
        commentmarkStart.setType(CommentMarkType.Comment_Start);
        
        //Create an end mark
        CommentMark commentmarkEnd = new CommentMark(doc);
        commentmarkEnd.setType(CommentMarkType.Comment_End);

        //Create a comment
        Comment comment = new Comment(doc);
        comment.getBody().addParagraph().setText("Developed by E-iceblue");
        comment.getFormat().setAuthor("Shaun");

        //Find the paragraph where the string is located
        Paragraph para = specificText.getAsOneRange().getOwnerParagraph();

        //Get the index of the string in the paragraph
        int index = para.getChildObjects().indexOf(specificText.getAsOneRange());

        //Add the comment to the paragraph
        para.getChildObjects().add(comment);
        
        //Insert the start mark and end mark to the paragraph based on the index
        para.getChildObjects().insert(index, commentmarkStart);
        para.getChildObjects().insert(index + 2, commentmarkEnd);
        
        //Save to file
        doc.saveToFile("AddComment.docx", FileFormat.Docx_2013);
    }
}

Java: Add Comments to Text in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Monday, 19 June 2023 01:54