Skip to main content

Add Comment to Specific Text and Get Marked Text of Specific Comment in Word in Java

 

A comment is a note or annotation that an author or reviewer can add to a document. In this article, I will illustrate how to add comment to specific text and get the marked text of a specific comment in a Word document using Java.

Add Dependencies

Free Spire.Doc for Java library are used to achieve the functionality. There are two ways to include Free Spire.Doc for Java in your Java project:

For maven projects, add the following dependencies to your project’s pom.xml file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<repositories>   
    <repository>   
        <id>com.e-iceblue</id>   
        <name>e-iceblue</name>   
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>   
    </repository>   
</repositories>   
<dependencies>   
    <dependency>   
        <groupId> e-iceblue </groupId>   
        <artifactId>spire.doc.free</artifactId>   
        <version>3.9.0</version>   
    </dependency>   
</dependencies>

The latest version of Free Spire.Doc for Java is 3.9.0 (at the time of writing this article).

For non-maven projects, download Free Spire.Doc for Java pack from here: Download- Free Spire.Doc for Java, extract the zip file, then add Spire.Doc.jar in the lib folder into your project as a dependency.

Add comment to specific text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
 
public class AddCommentToSpecificText {
    public static void main(String[] args){
        //Create word document
        Document document = new Document();
        //Load the document from disk
        document.loadFromFile("Template.docx");
 
        //Call insertComments method to add comment to the specific text "Dragon Boat Festival"
        insertComments(document, "Dragon Boat Festival");
 
        //Save the document
        document.saveToFile("addCommentToSpecificText.docx", FileFormat.Docx_2013);
    }
    private static void insertComments(Document doc, String stringToComment) {
        //Find the string
        TextSelection find = doc.findString(stringToComment, false, true);
 
        //Create comment start mark and comment end mark
        CommentMark commentMarkStart = new CommentMark(doc);
        commentMarkStart.setType(CommentMarkType.Comment_Start);
        CommentMark commentMarkEnd = new CommentMark(doc);
        commentMarkEnd.setType(CommentMarkType.Comment_End);
 
        //Add content for comment
        Comment comment = new Comment(doc);
        comment.getBody().addParagraph().setText("Also known as Duanwu festival");
        //Set the author of comment
        comment.getFormat().setAuthor("Sierra");
 
        //Get the textRange
        TextRange range = find.getAsOneRange();
 
        //Get its paragraph
        Paragraph para = range.getOwnerParagraph();
        //Get the index of textRange
        int index = para.getChildObjects().indexOf(range);
        //Add comment to the paragraph
        para.getChildObjects().add(comment);
        //Insert the comment start mark and comment end mark to the paragraph
        para.getChildObjects().insert(index, commentMarkStart);
        para.getChildObjects().insert(index + 2, commentMarkEnd);
    }
}

Output:

Get the marked text of a specific comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import com.spire.doc.Document;
import com.spire.doc.documents.CommentMark;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
import com.spire.doc.fields.TextRange;
 
 
public class GetMarkedTextOfSpecificComment {
    public static void main(String[] args){
        //Load the Word document
        Document doc = new Document();
        doc.loadFromFile("addCommentToSpecificText.docx");
 
        //Get the first comment in the document
        Comment comment = doc.getComments().get(0);
         
        //get the start mark and end mark of the comment
        Paragraph para = comment.getOwnerParagraph();
        CommentMark start = comment.getCommentMarkStart();
        CommentMark end = comment.getCommentMarkEnd();
        int indexOfStart = para.getChildObjects().indexOf(start);
        int indexOfEnd = para.getChildObjects().indexOf(end);
 
        String markedText = "";
        //Get the marked text between the comment start mark and comment end mark
        for (int i = indexOfStart + 1; i < indexOfEnd; i++) {
            if (para.getChildObjects().get(i) instanceof TextRange) {
                TextRange range = (TextRange) para.getChildObjects().get(i);
                markedText += range.getText();
            }
        }
         
        //Print out the marked text
        System.out.println(markedText);
    }
}

Output:

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url>                </repository>       </repositories>       <dependencies>           <dependency>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>               </repository>       </repositories>       <dependencies>           <dependency>               <groupId>