In this digital world, almost everyone is aware of and is using the popular MS Word (DOC/DOCX) and PDF documents. Both formats have their pros and cons. Everyone uses these document formats according to one’s needs. Such extensive usage increases the requirement to convert these formats into each other. In this article, we will learn how to convert MS Word documents (DOC/DOCX) files into PDF format in Java. Further, we will see how to transform specific pages of any password-protected Word document within the Java application.

Convert Word Document to PDF in Java.

Java API to Convert Word Files to PDF

GroupDocs has its GroupDocs.Conversion solution for Java developers. It helps programmers in the conversion of various documents and image formats with its efficient and robust Java API. In this article, I will use its GroupDocs.Conversion for Java API to convert the DOC/DOCX documents into PDF format.

You can download the JAR file from the downloads section or use the latest repository and dependency configurations within your Maven-based Java applications.

<repository>
    <id>groupdocs-artifacts-repository</id>
    <name>GroupDocs Artifacts Repository</name>
    <url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-conversion</artifactId>
    <version>22.12</version>
</dependency>

How to Convert Word (DOC/DOCX) to PDF in Java

Here we will see the basic conversion of a Word document into a PDF in Java. The following steps transform all the pages of a DOCX file into a PDF file in Java.

  • Load the Word DOC/DOCX file using the Converter class.
  • Transform the loaded document into PDF format using the convert method.
  • That’s it.

The following Java code converts the whole Word document into PDF format.

Convert Selected Pages of Password-Protected Word Documents in Java

Similarly, you can also convert locked and protected Word documents into other formats. The following steps show how to convert selective pages of a password-protected Word document into PDF format in Java.

  • Prepare the loading options using the WordProcessingLoadOptions class.
  • Now, load the Word DOC/DOCX file using the Converter class.
  • Prepare the conversion options for the PDF file using the PdfConvertOptions class.
  • Prepare the list of targeted pages that needs to get converted using setPages, setPageNumber, and setPageCount methods.
  • Convert the loaded Word file into PDF format using the convert method.

The following Java code converts the selected pages of the password-protected Word document into PDF format.

Conclusion

To sum up, today we discussed the conversion the MS Word (DOC/DOCX) files to PDF format in Java. Likewise, we also converted the selected pages of a password-protected document into PDF within the Java application.

You can learn more about the Document Conversion Automation API using its documentation, API Reference, or by experiencing its GitHub examples. You can reach us for any query via the forum.

Related Article

See Also