Convert Word to PDF in Java

Word to PDF conversion is commonly used before the documents are shared. Various online Word to PDF converters are available that allow you to convert single or a limited number of Word documents. However, with emerging MS Word automation and report generation solutions, automated Word to PDF conversion has become an essential part of the systems. Furthermore, the batch conversion of DOC/DOCX to PDF needs to be done automatically to minimize the time and effort. Keeping an eye on such scenarios, I’ll show you how to automate the process of converting Word DOC or DOCX documents to PDF programmatically in Java.

Java Word to PDF Conversion Scenarios

You’ll learn about the following Word (DOC/DOCX) to PDF conversions in this article.

Java Library to Convert Word to PDF

For converting Word DOCX/DOC to PDF, we’ll use Aspose.Words for Java which is a powerful Word automation API for working with popular word processing formats. You can either download Aspose.Words for Java or install it in your Maven-based application using the following configurations.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>23.6</version>
    <classifier>jdk17</classifier>
</dependency>

Convert Word DOC/DOCX to PDF in Java

In order to convert a Word document to PDF, you simply need to load the Word document and save it with “.pdf” extension. The following are the steps to convert DOCX/DOC to PDF in Java.

The following code sample shows how to convert a Word DOC to PDF in Java.

Word Document

DOC DOCX to PDF in Java

PDF Document

DOCX to PDF

Convert Selected Pages of Word DOC/DOCX to PDF in Java

In case you want to convert only selective pages of Word to PDF, You can do it using the PdfSaveOptions class. You can either convert the first N pages or a range of pages by specifying the starting page’s index.

The following are the steps to convert selected pages of a Word DOCX/DOC to PDF in Java.

  • Load the Word document using Document class.
  • Create an instance of PdfSaveOptions class.
  • Set starting page’s index and the number of pages to convert.
  • Save the Word document as PDF using Document.save() method.

The following code sample shows how to convert selected pages of Word DOC/DOCX to PDF in Java.

Java Word DOC/DOCX to PDF with Compliance

There exist various PDF standards a PDF document may compliant with, such as PDF/A-1a, PDF 1.5, etc. In case you want to convert Word document to a specific PDF standard, you can specify it using PdfCompliance.

The following code sample shows how to convert Word DOCX to PDF with a particular PDF standard in Java.

Apply Text or Image Compression in Word to PDF Conversion

You can also reduce the size of the resultant PDF document by compressing the text or image in the Word document. You can apply the following compressions in Word to PDF conversion using Aspose.Words for Java.

Text Compression Options

Aspose.Words for Java provides PdfTextCompression class with the following text compression options:

  • None: No text compression.
  • Flate: Flate (ZIP) compression.

Image Compression

The image compression options are available in the PdfImageCompression class.

  • Auto: The most appropriate compression for every image is selected automatically.
  • Jpeg: Images are converted to JPEG format (does not support transparency).

The following code sample shows how to convert Word DOCX to PDF by applying text and image compression in Java.

Convert Word to PDF with JPEG Quality in Java

You can also customize and control the JPEG quality in Word to PDF conversion. The PdfSaveOptions.setJpegQuality is used to set JPEG quality which can be from 0 (worst quality with maximum compression) to 100 (best quality with minimum compression).

The following code sample shows how to specify JPEG quality while converting Word DOCX to PDF in Java.

Get a Free License

You can get a free temporary license to try and use Aspose.Words for Java.

Conclusion

In this blog post, we’ve explored the process of converting Word documents to PDF in Java using Aspose.Words for Java. This powerful library makes it easy for Java developers to work with Word documents and perform conversions between various formats seamlessly. By following the steps outlined in this guide, you can integrate this functionality into your Java applications and improve document handling and management. You can explore more about Aspose.Words for Java using the documentation.

Info: Aspose provides a free online web app that allows you to view PDFs online, another that allows you to convert PDFs to video, and one that allows you to edit PDFs online.

Info: You may be interested in another Java API, especially one that allows developers and applications to convert PowerPoint to PDF – Aspose.Slides for Python. And you might also want to check out Aspose PowerPoint to PDF Converter because it is a live implementation of the PowerPoint document to PDF process.