Converting a document from PDF to DOCX format
Use PDF-to-Word conversion when you need editable content from existing PDF files. Common use cases include:
- Updating contract templates
- Reusing document content in new files
- Enabling collaborative editing in Word
- Keeping conversion inside your app for sensitive documents
Use the Java SDK for conversion
Use the Java SDK to convert PDF files to DOCX directly in your document workflow.
Preparing the project
Define a package and create a class for the conversion flow:
package io.nutrient.Sample;Import Nutrient Java SDK classes. Prefer explicit imports for the classes you use:
import io.nutrient.sdk.Document;import io.nutrient.sdk.exceptions.NutrientException;
public class PDFToWordDocument {Create a main method and declare NutrientException:
public static void main(String[] args) throws NutrientException {Then add the SDK-specific conversion logic.
Proceeding with the conversion
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.pdf")) {The path can be absolute or relative. This example reads from the working directory.
After loading the PDF, call SDK methods on the document instance. For the full API surface, refer to the API reference.
Export the document as Word. Write to a file path or stream. This example writes output.docx to the working directory:
document.exportAsWord("output.docx"); } }}You now have an editable Word document.
Error handling
The SDK throws NutrientException when conversion fails. Handle this exception in your app for custom logging, retries, or fallback logic.
Conclusion
You now have a complete PDF-to-DOCX conversion flow in Java. Download the sample package to run this example as-is.