Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created August 12, 2020 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/ef9c6086cf6e7030afda888475736625 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ef9c6086cf6e7030afda888475736625 to your computer and use it in GitHub Desktop.
Insert, delete, remove comments from Word documents DOC/DOCX
// Load source word document
Document doc = new Document(dataDir + "Comments.docx");
// Initialize DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Create new comment
Comment comment = new Comment(doc, "Aspose", "Initials", new java.util.Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new com.aspose.words.Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new com.aspose.words.Run(doc, "Sample Comment"));
// Save output file
doc.save(dataDir + "Comments_Output.docx");
// Initialize new word document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Add some text
builder.write("Some text is added.");
// Create new comment
Comment comment = new Comment(doc, "Aspose", "Initials", new java.util.Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new com.aspose.words.Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new com.aspose.words.Run(doc, "Sample Comment"));
// Save output DOCX file
doc.save(dataDir + "Comments_Output.docx");
// Open the document.
Document doc = new Document(dataDir + "Comments.docx");
// Collect all comments in the document
NodeCollection comments = doc.getChildNodes(com.aspose.words.NodeType.COMMENT, true);
// Remove all comments.
comments.clear();
doc.save(dataDir + "output.docx");
// Open the document.
Document doc = new Document(dataDir + "Comments.docx");
String authorName = "Aspose";
// Collect all comments in the document
NodeCollection comments = doc.getChildNodes(NodeType.COMMENT, true);
// Look through all comments and remove those written by the Aspose author.
for (int i = comments.getCount() - 1; i >= 0; i--) {
Comment comment = (Comment) comments.get(i);
if (comment.getAuthor().equals(authorName))
comment.remove();
}
// Save output DOCX file
doc.save(dataDir + "output.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment