Converting a Word document to PDF while preserving comments
In collaborative business environments, Microsoft Word documents often contain comments and annotations from multiple reviewers. These comments provide context, feedback, and revision history that’s important for understanding document development. However, when converting Word documents to PDF, comments are typically lost unless specifically preserved.
PDF conversion tools typically strip out comments during the conversion process. This creates challenges for organizations that need to maintain complete document history, preserve reviewer feedback, or comply with audit requirements that mandate retention of all document annotations.
Nutrient Java SDK enables you to convert Word documents to PDF while preserving all comments and annotations in the resulting PDF file.
Implementing comment preservation
This feature can be implemented using Nutrient Java SDK with a few lines of code and a configuration setting. You can integrate DOCX to PDF conversion with full comments preservation directly into your applications. Whether you’re building a document review system or adding export functionality with collaborative features, Nutrient Java SDK provides a solution that preserves document structure.
Preparing the project
Create a new class named WordDocumentToPDFIncludingComments
with the following structure. Import the necessary classes from Nutrient Java SDK.
package io.nutrient.Sample;
import io.nutrient.sdk.Document;import io.nutrient.sdk.enums.DocumentMarkupMode;import io.nutrient.sdk.exceptions.NutrientException;
public class WordDocumentToPDFIncludingComments { public static void main(String[] args) throws NutrientException { // Document opening, configuration, and conversion code will be added below }}
The main
method throws a NutrientException
, which you can catch in your application logic for custom error management if desired.
Configuring comment preservation
With the SDK successfully initialized, you can begin working with the classes it offers. In this guide, we’ll concentrate on the Document
class. You can initialize Document
using a try-with-resources statement(opens in a new tab), which ensures proper lifecycle management of the document instance.
The SDK supports multiple integration methods, enabling flexibility in how you connect with your application. The source file can be specified either using a file path or a stream. In this guide, we’ll focus on using a file path as the source.
try (Document document = Document.open("input_with_comments.docx")) {
To preserve comments and markups during conversion, set the MarkupMode
enum value to the required mode. This instructs the SDK to include the proper markup mode (user comments, modification suggestions) in the resulting PDF file.
The MarkupMode
enum has four values that control how comments and markups appear in the PDF:
Mode | Changes displayed | Comments converted to annotations |
---|---|---|
AllMarkup | All markups shown in different colors, underlined/struck through | Yes (default) |
SimpleMarkup | Document shown as if all changes were accepted | Yes |
NoMarkup | Document shown as if all changes were accepted | No |
Original | Document shown as if all changes were rejected | No |
document.getSettings().getWordSettings().setMarkupMode(DocumentMarkupMode.AllMarkup);
If your input document doesn’t contain any comments, the conversion will still complete successfully. The
MarkupMode
setting only affects how comments and tracked changes are rendered when they exist in the source document.
Proceeding with the conversion
Export this document to PDF while preserving all comments. Similar to when opening the DOCX file, where we could choose between using a file path or a stream, the saving functionality offers the same flexibility. In this case, we’ll save the PDF as output.pdf
in the application’s working directory.
document.exportAsPdf("output.pdf"); } }}
You now have a PDF file with all comments preserved.
Error handling
Nutrient Java SDK uses exception handling to report errors. Both the Document.open()
and exportAsPdf()
methods throw a NutrientException
when they fail.
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
To implement custom error handling, wrap the conversion code in a try-catch block:
try (Document document = Document.open("input_with_comments.docx")) { document.getSettings().getWordSettings().setMarkupMode(DocumentMarkupMode.AllMarkup); document.exportAsPdf("output.pdf");} catch (NutrientException e) { System.err.println("Conversion failed: " + e.getMessage()); // Implement your error handling logic here}
Conclusion
That’s all it takes to convert a DOCX document into a PDF file while preserving all comments and annotations. This feature ensures that collaborative document history remains intact and accessible in the final PDF format.
What's next
- Refer to the Word to PDF conversion guide to learn about basic conversion without comments
- Refer to the Excel to PDF guide to convert spreadsheet documents
- Refer to the PowerPoint to PDF guide to convert presentation documents