Use Apache POI’s XWPFDocument to read comments from a DOCX file, then iterate over the returned XWPFComment objects to collect their text and metadata. A comment is stored in the document’s comments part, so it is not reliably retrieved by scanning ordinary document paragraphs.
Read DOCX comments with Apache POI
Apache POI’s XWPF API provides a direct route from the document to its comments. The example below opens a file, handles a missing comments collection, and prints each comment’s ID, author, date, and text.
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.poi.xwpf.usermodel.XWPFComment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class ReadDocxComments {
public static void main(String[] args) throws Exception {
Path file = Path.of("input.docx");
try (InputStream in = Files.newInputStream(file);
XWPFDocument doc = new XWPFDocument(in)) {
XWPFComment[] comments = doc.getComments();
if (comments == null || comments.length == 0) {
System.out.println("No comments found.");
return;
}
for (XWPFComment comment : comments) {
System.out.printf("ID: %s%nAuthor: %s%nDate: %s%nText: %s%n%n",
comment.getId(), comment.getAuthor(), comment.getDate(),
comment.getText());
}
}
}
}
Use the Apache POI XWPF comments API documentation for the release selected by your project: XWPFDocument and XWPFComment. Confirm the constructor and null behavior against that release rather than assuming they are identical across versions.
What the code does
- Opens the DOCX as an input stream and constructs an
XWPFDocument. - Calls
getComments()to obtain comment objects. A document with no comments may have no comments collection, so check fornullas well as an empty array. - Reads each comment’s ID, author, date, and text through the
XWPFCommentAPI. - Closes both the stream and document through try-with-resources.
Access the comments part directly
If you need the comments container itself, call getDocComments() on the document and then call getComments() on the returned XWPFComments part. The part also provides operations such as looking up a comment by ID, creating a comment, or removing one. See the XWPFComments API documentation.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
You can retrieve a particular comment with getCommentByID(String) on the document or comments part. This is useful when another part of your application has already identified a comment ID and does not need to enumerate every comment.
Preserve comment structure when needed
getText() is convenient when you need a plain-text value, but a comment can contain multiple paragraphs or other body elements. If paragraph boundaries or richer structure matter to your application, traverse the comment’s getParagraphs() or getBodyElements() instead of treating the comment as one undifferentiated string. The available methods are listed in the XWPFComment documentation.
Rank #2
- The Microsoft Office 365 Bible: The Most Updated and Complete Guide to Excel, Word, PowerPoint, Outlook, OneNote, OneDrive, Teams, Access, and Publisher from Beginners to Advanced
- ABIS BOOK
Use Aspose.Words for a higher-level alternative
Aspose.Words for Java exposes comments as com.aspose.words.Comment nodes. Its guide demonstrates recursively collecting them with doc.getChildNodes(NodeType.COMMENT, true), then reading author, date/time, and text. The same API supports filtering by author and removing comments. See the Aspose.Words guide to working with comments and the Comment API reference.
Apache POI is a fit when an open-source dependency and direct access to OOXML-oriented document parts suit the project. Aspose.Words may suit applications that benefit from its higher-level document model and documented recursive traversal or comment workflows. Compare licensing, feature support for the files you handle, dependency footprint, and maintenance needs; the cited documentation does not establish a performance winner.
Quick Recap
Best Value
Rank #4
Rank #3
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

