Tuesday, 14 May 2019 07:53

Java add, delete and reply to comment on word document

Written by  support iceblue
Rate this item
(0 votes)

This article is going to demonstrate how to work with comment in a Word document using Spire.Doc for Java. Spire.Doc supports to add new comment to word document and insert a comment as a reply to a selected comment, it also supports to delete the existing comment from a word document in Java applications.

Add a new comment to word document

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;


public class WordComment {
    public static void main(String[] args) throws Exception{

            String inputFile="Sample.docx";
            String outputFile="out/Comment.docx";

            //load a word document
            Document document= new Document(inputFile);

            Section section = document.getSections().get(0);
            Paragraph paragraph = section.getParagraphs().get(1);

            //Insert a new comment
            Comment comment = paragraph.appendComment("Spire.Doc for Java");
            comment.getFormat().setAuthor("E-iceblue");
            comment.getFormat().setInitial("CM");

            //save the file
            document.saveToFile(outputFile, FileFormat.Docx);
        }
    }

Java add, delete and reply to comment on word document

Java reply to a comment

import com.spire.doc.*;
import com.spire.doc.FileFormat;
import com.spire.doc.fields.*;

public class WordComment {
    public static void main(String[] args) throws Exception{

            String inputFile="Sample.docx";
            String outputFile="out/ReplaytoComment.docx";

            //load a word document
            Document document= new Document(inputFile);

            Comment comment1 = document.getComments().get(0);

            Comment replyComment1 = new Comment(document);

            replyComment1.getFormat().setAuthor("E-iceblue");
            replyComment1.getBody().addParagraph().appendText("Spire.Doc for Java is a professional Word Java library on operating Word documents.");

            //add the new comment as a reply to the selected comment.
            comment1.replyToComment(replyComment1);
            //save the file
            document.saveToFile(outputFile, FileFormat.Docx);
        }
    }

Java add, delete and reply to comment on word document

Delete comment

import com.spire.doc.*;
import com.spire.doc.FileFormat;

public class WordComment{
    public static void main(String[] args) throws Exception{

            String inputFile="Sample.docx";
            String outputFile="out/DeleteComment.docx";

            //load a word document
            Document document= new Document(inputFile);

            //remove the second comment
            document.getComments().removeAt(1);

            //save the file.
            document.saveToFile(outputFile, FileFormat.Docx);
        }
    }

Java add, delete and reply to comment on word document

Additional Info

  • tutorial_title:
Last modified on Tuesday, 14 May 2019 07:57