Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active November 26, 2021 16:20
This code can be used to add comments in Word file using Java. More details are available here: https://kb.aspose.com/words/java/how-to-add-comments-in-word-using-java/
import com.aspose.words.License;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Comment;
import com.aspose.words.Paragraph;
import com.aspose.words.Run;
import java.util.Date;
public class HowToAddCommentsInWordUsingJava
{
public static void main(String[] args) throws Exception { //main function for AddImageInWord class
// Initialize a license to avoid trial version watermark in the output Word file after adding image
License license = new License();
license.setLicense("Aspose.Words.lic");
// Load the Word document where comments are to be added
Document DocumentForComment = new Document("input.docx");
DocumentBuilder builder = new DocumentBuilder(DocumentForComment);
// Move the cursor to the beginning of the document for adding comments
builder.moveToDocumentStart();
// Insert comment to first paragraph of document by providing Author, Initial, time and comment text
Comment comment = new Comment(DocumentForComment, "Aspose.Words", "AW", new Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new Paragraph(DocumentForComment));
comment.getFirstParagraph().getRuns().add(new Run(DocumentForComment, "Comment text."));
// Save the Document with comments
DocumentForComment.save("OutputDocumentWithComments.docx");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment