groupdocs word to pdf: Конвертировать защищённый Word в PDF на Java

В этом руководстве вы узнаете, как выполнить конвертацию groupdocs word to pdf в Java, превращая защищённые паролем документы Word в PDF высокого качества без усилий. Мы пройдём настройку диапазонов страниц, регулировку DPI, вращение страниц и точную настройку размеров, чтобы вы могли адаптировать результат под свои точные требования.

Quick Answers

  • What library handles the conversion? GroupDocs.Conversion for Java.
  • Can I convert a password‑protected Word file? Yes – just supply the password via WordProcessingLoadOptions.
  • How do I limit the conversion to specific pages? Use setPageNumber() and setPagesCount() on PdfConvertOptions.
  • 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 groupdocs word to pdf conversion?

GroupDocs.Conversion is a Java library that transforms Word documents (including protected ones) into PDF files. It abstracts the low‑level parsing and rendering work, letting you focus on business logic such as security handling, page selection, and output quality.

Why use GroupDocs for Java convert word pdf tasks?

  • Zero‑install – pure Java, no native binaries.
  • Password support – open encrypted documents safely.
  • Fine‑grained control – page ranges, DPI, rotation, and custom dimensions.
  • Scalable performance – optimized for large files and server‑side workloads.

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

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

Overview: Turn a secured Word file into a PDF with a single call.

Step‑by‑Step Implementation

  1. Initialize Load Options with Password – supply the correct password.
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
loadOptions.setPassword("12345"); // Replace with your actual password.
  1. 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.

Troubleshooting Tips

  • Verify the password; a typo triggers an IncorrectPasswordException.
  • Use absolute paths or ensure the working directory matches the relative paths to avoid FileNotFoundException.

Specify Pages to Convert in PDF

Overview: Convert only the pages you need, saving time and storage.

Step‑by‑Step Implementation

  1. 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.
  1. Conversion Process – reuse the same Converter instance.
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

Overview: Adjust page orientation directly during conversion.

Step‑by‑Step Implementation

  1. 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.
  1. 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

Overview: Control the resolution of images and vector graphics inside the PDF.

Step‑by‑Step Implementation

  1. Configure DPI Settings
PdfConvertOptions options = new PdfConvertOptions();
options.setDpi(300); // Set DPI to 300 for high resolution.
  1. 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

Overview: Define explicit pixel dimensions for the output PDF.

Step‑by‑Step Implementation

  1. Define Dimensions
PdfConvertOptions options = new PdfConvertOptions();
options.setWidth(1024); // Set width to 1024 pixels.
options.setHeight(768); // Set height to 768 pixels.
  1. 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 specific screen sizes or print formats.

Common Issues and Solutions

IssueLikely CauseFix
IncorrectPasswordExceptionWrong password suppliedDouble‑check the password string; trim whitespace.
FileNotFoundExceptionInvalid file pathUse absolute paths or verify the working directory.
Output PDF is blurryDPI too lowIncrease DPI via options.setDpi().
Pages appear upside‑downRotation not set or set incorrectlyUse options.setRotate(Rotation.On180) (or other enum).
Converted file is larger than expectedHigh DPI + large dimensionsLower 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, consider increasing JVM heap size and using the Converter.dispose() method when done.

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.

Conclusion

You now have a complete, production‑ready roadmap for performing a groupdocs word to pdf conversion in Java, including handling password protection, page selection, rotation, DPI, and custom dimensions. Combine these snippets to fit your specific workflow, and you’ll be able to deliver PDFs that meet exact business requirements.


Last Updated: 2026-03-06
Tested With: GroupDocs.Conversion 25.2 for Java
Author: GroupDocs