Working with comments in docx4j and Aspose.Words

Aspose.Words - Working with comments

Comments of the document are represented by the Comment class.

Java

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Some text is added.");
Comment comment = new Comment(doc, "Aspose", "As", new Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text."));
doc.save(dataDir + "Aspose_Comments.docx");

docx4j - Working with comments

Creates a WordprocessingML document from scratch, and adds a comment.

Java

outputfilepath = dataDir + "Docx4j_CommentsSample.docx";
boolean save = (outputfilepath == null ? false : true);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
		.createPackage();

// Create and add a Comments Part
CommentsPart cp = new CommentsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(cp);

// Part must have minimal contents
Comments comments = factory.createComments();
cp.setJaxbElement(comments);

// Add a comment to the comments part
java.math.BigInteger commentId = BigInteger.valueOf(0);
Comment theComment = createComment(commentId, "fred", null,
		"my first comment");
comments.getComment().add(theComment);

// Add comment reference to document
P paraToCommentOn = wordMLPackage.getMainDocumentPart()
		.addParagraphOfText("here is some content");
paraToCommentOn.getContent().add(createRunCommentReference(commentId));

// ++, for next comment ...
commentId = commentId.add(java.math.BigInteger.ONE);
wordMLPackage.save(new java.io.File(outputfilepath));

Download Running Code

Download Sample Code