Created
August 12, 2020 23:31
-
-
Save aspose-com-gists/ef9c6086cf6e7030afda888475736625 to your computer and use it in GitHub Desktop.
Insert, delete, remove comments from Word documents DOC/DOCX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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