Word to PDF java: Convert protected Word files with GroupDocs
In this comprehensive tutorial you’ll learn how to perform a word to pdf java conversion using GroupDocs.Conversion. We’ll walk through opening password‑protected Word documents, selecting specific page ranges, adjusting DPI, rotating pages, and customizing dimensions so the resulting PDF matches your exact requirements.
Quick answers
- What library handles the conversion? GroupDocs.Conversion for Java.
- Can I convert a password‑protected Word file? Yes – provide the password via
WordProcessingLoadOptions. - How do I limit the conversion to specific pages? Use
setPageNumber()andsetPagesCount()onPdfConvertOptions. - Is DPI configurable? Absolutely; call
options.setDpi(yourValue). - Do I need Maven to add GroupDocs? Yes – include the Maven repository and dependency (see the Maven groupdocs dependency section).
What is word to pdf java conversion?
Word to pdf java conversion is the process of transforming a Microsoft Word document into a PDF file using Java code. GroupDocs.Conversion abstracts the complex rendering logic, letting you focus on business rules such as security handling and output quality.
Why use GroupDocs for Java convert word pdf tasks?
GroupDocs.Conversion supports 50+ input and output formats, processes multi‑hundred‑page documents without loading the whole file into memory, and runs on pure Java—no native binaries required. This makes it ideal for high‑throughput server environments where stability and speed matter. It also integrates easily with existing Java applications.
Prerequisites
- JDK 8 or newer installed and configured.
- Basic Java development experience.
- Access to a GroupDocs.Conversion license (free trial available).
Required libraries and dependencies
To use GroupDocs.Conversion, include the Maven repository and dependency in your pom.xml:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/conversion/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-conversion</artifactId>
<version>25.2</version>
</dependency>
</dependencies>
License acquisition
GroupDocs.Conversion offers a free trial version for testing features. For extended use, consider acquiring a temporary or full license from GroupDocs Purchase.
Setting up GroupDocs.Conversion for Java
Maven setup
The Maven snippet above ensures all required JARs are downloaded automatically.
Basic initialization
The Converter class is the entry point that orchestrates document loading and conversion.
Create a Converter instance and load a protected document:
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.load.WordProcessingLoadOptions;
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
// Set password for protected documents if necessary:
loadOptions.setPassword("your_password_here");
Converter converter = new Converter("path_to_your_document.docx", () -> loadOptions);
The loadOptions object is where you handle the convert password protected word scenario.
Implementation guide
Below we dive into each feature you might need for a robust java convert word pdf workflow.
Convert password‑protected document to PDF
Definition: WordProcessingLoadOptions specifies options for loading Word documents, including the password for encrypted files.
Definition: PdfConvertOptions defines PDF output settings such as page range, DPI, rotation, and dimensions.
Direct answer: Load the Word file with new Converter("input.docx", new WordProcessingLoadOptions("password")) and then call converter.convert(new PdfConvertOptions(), "output.pdf") – the library unlocks the document and produces a PDF in a single step.
Step‑by‑step implementation
- Initialize load options with password – supply the correct password.
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
loadOptions.setPassword("12345"); // Replace with your actual password.
- Set up converter and convert – define PDF options and execute.
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
String convertedFile = "YOUR_OUTPUT_DIRECTORY/ConvertedDocument.pdf";
PdfConvertOptions options = new PdfConvertOptions();
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleProtectedDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);
Explanation: The loadOptions object unlocks the document, while PdfConvertOptions lets you tweak the output later if needed.
Specify pages to convert in PDF
Direct answer: Use PdfConvertOptions.setPageNumber(startPage) and setPagesCount(pageCount) to tell GroupDocs which pages to render, then run the conversion as usual.
Step‑by‑step implementation
- Set page range – tell the converter which pages to render.
PdfConvertOptions options = new PdfConvertOptions();
options.setPageNumber(2); // Start from page 2.
options.setPagesCount(1); // Convert only one page.
- Conversion process – reuse the same
Converterinstance.
String convertedFile = "YOUR_OUTPUT_DIRECTORY/SelectedPagesPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);
Explanation: setPageNumber() defines the first page, while setPagesCount() limits how many pages are processed.
Rotate pages in PDF conversion
Direct answer: Call PdfConvertOptions.setRotate(Rotation.On90) (or another enum value) before conversion to rotate every output page by the chosen angle.
Step‑by‑step implementation
- Set rotation options – choose a rotation enum.
import com.groupdocs.conversion.options.convert.Rotation;
PdfConvertOptions options = new PdfConvertOptions();
options.setRotate(Rotation.On180); // Rotate pages 180 degrees.
- Execute conversion – same pattern as before.
String convertedFile = "YOUR_OUTPUT_DIRECTORY/RotatedPagesPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);
Explanation: Rotating can fix landscape scans or meet specific layout requirements.
Set DPI for PDF conversion
Direct answer: Adjust image resolution with PdfConvertOptions.setDpi(300) (or any integer) before calling convert; higher DPI yields sharper graphics at the cost of larger file size.
Step‑by‑step implementation
- Configure DPI settings
PdfConvertOptions options = new PdfConvertOptions();
options.setDpi(300); // Set DPI to 300 for high resolution.
- Perform conversion with custom DPI
String convertedFile = "YOUR_OUTPUT_DIRECTORY/HighResolutionPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);
Explanation: Higher DPI improves visual fidelity but increases file size—choose based on your target medium.
Set width and height for PDF conversion
Direct answer: Define explicit pixel dimensions via PdfConvertOptions.setWidth(1240) and setHeight(1754) to force the output PDF to match a specific page size.
Step‑by‑step implementation
- Define dimensions
PdfConvertOptions options = new PdfConvertOptions();
options.setWidth(1024); // Set width to 1024 pixels.
options.setHeight(768); // Set height to 768 pixels.
- Convert with custom sizes
String convertedFile = "YOUR_OUTPUT_DIRECTORY/SizedPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);
Explanation: Custom dimensions are handy for generating PDFs that fit particular screen sizes or print formats.
How to convert Word to PDF java using GroupDocs?
Load your protected Word file with new Converter("doc.docx", new WordProcessingLoadOptions("pwd")), configure any PdfConvertOptions you need (pages, DPI, rotation, size), and invoke converter.convert(options, "output.pdf"). This single‑line pattern handles decryption, rendering, and file writing, delivering a production‑ready PDF without external tools. It works on any platform that supports Java 8 or later.
Common issues and solutions
| Issue | Likely cause | Fix |
|---|---|---|
IncorrectPasswordException | Wrong password supplied | Double‑check the password string; trim whitespace. |
FileNotFoundException | Invalid file path | Use absolute paths or verify the working directory. |
| Output PDF is blurry | DPI too low | Increase DPI via options.setDpi(). |
| Pages appear upside‑down | Rotation not set or set incorrectly | Use options.setRotate(Rotation.On180) (or other enum). |
| Converted file is larger than expected | High DPI + large dimensions | Lower DPI or adjust width/height to balance size vs. quality. |
Frequently asked questions
Q: Can I convert a Word document that has both a password and read‑only protection?
A: Yes. Supply the opening password via WordProcessingLoadOptions.setPassword(). Read‑only flags are ignored during conversion.
Q: Does GroupDocs.Conversion support .doc (legacy) files as well as .docx?
A: Absolutely. The library handles both formats transparently.
Q: How does the java convert word pdf performance scale with large files?
A: GroupDocs streams data and releases resources after each conversion. For very large files, increase JVM heap size and call Converter.dispose() when finished.
Q: Is it possible to convert multiple documents in a batch?
A: Yes. Loop over file paths, create a new Converter for each, and reuse the same PdfConvertOptions where appropriate.
Q: Do I need a commercial license for development builds?
A: A free trial works for evaluation, but production deployments require a valid GroupDocs.Conversion license.
Last Updated: 2026-10-10
Tested With: GroupDocs.Conversion 25.2 for Java
Author: GroupDocs