Converting a Word document to PDF while preserving comments
Word review workflows often rely on comments and tracked changes. By default, many DOCX-to-PDF conversions remove this markup.
Use this workflow to:
- Preserve reviewer comments in the output PDF
- Keep tracked-change context for audits and approvals
- Export collaboration history together with final content
Implementing comment preservation
Use Nutrient Java SDK to convert DOCX to PDF and control how comments and tracked changes are rendered.
Preparing the project
Create a new class named WordDocumentToPDFIncludingComments with the following structure:
package io.nutrient.Sample;Import the necessary classes from Nutrient Java SDK:
import io.nutrient.sdk.Document;import io.nutrient.sdk.enums.DocumentMarkupMode;import io.nutrient.sdk.exceptions.NutrientException;
public class WordDocumentToPDFIncludingComments {The main method throws a NutrientException. Catch it in your application logic for custom error management:
public static void main(String[] args) throws NutrientException {Configuring comment preservation
This guide uses the Document class. Initialize it with a try-with-resources statement(opens in a new tab) to close resources correctly.
Open source files by file path or stream. This example uses a file path:
try (Document document = Document.open("input_with_comments.docx")) {To preserve comments and markups during conversion, set the appropriate DocumentMarkupMode enum value.
DocumentMarkupMode has four values that control how comments and markups appear in the PDF:
DocumentMarkupMode value | Changes displayed | Comments converted to annotations |
|---|---|---|
DocumentMarkupMode.AllMarkup | All markups shown in different colors, underlined/struck through | Yes (default) |
DocumentMarkupMode.SimpleMarkup | Document shown as if all changes were accepted | Yes |
DocumentMarkupMode.NoMarkup | Document shown as if all changes were accepted | No |
DocumentMarkupMode.Original | Document shown as if all changes were rejected | No |
document.getSettings().getWordSettings().setMarkupMode(DocumentMarkupMode.AllMarkup);If your input file doesn’t include comments, conversion still succeeds.
DocumentMarkupModeonly affects files that contain comments or tracked changes.
Proceeding with the conversion
Export the document to PDF. Write to a file path or stream. This example writes output.pdf to the working directory:
document.exportAsPdf("output.pdf"); } }}You now have a PDF file with comments preserved.
Error handling
The SDK throws NutrientException when Document.open() or exportAsPdf() fails.
Common error scenarios include:
- Input file not found or inaccessible
- Unsupported file format
- Insufficient memory for large documents
- Write permission issues for the output directory
Conclusion
You now have a DOCX-to-PDF conversion flow that preserves comments and annotations.
What’s next
- Refer to the Word-to-PDF conversion guide to learn about basic conversion without comments
- Refer to the Excel-to-PDF conversion guide to convert spreadsheet documents
- Refer to the PowerPoint-to-PDF conversion guide to convert presentation documents