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 efforts. Keeping an eye on such scenarios, I’ll show you how to automate the process of converting Word (DOC/DOCX) documents to PDF programmatically in Java.
Word to PDF Conversion Scenarios
You’ll learn about the following Word (DOC/DOCX) to PDF conversions in this article.
- Convert a Word document to PDF in Java.
- Convert selected pages of a Word DOC/DOCX to PDF in Java.
- Convert Word to PDF with a specific PDF Standard such as PDF 1.5, PDF/A-1a, etc.
- Convert Word to PDF with image/text compression in Java.
- Convert Word to PDF with the customized JPEG quality for images in Java.
Java Word API for Word to PDF Conversion
For converting Word documents to PDF, we’ll use Aspose.Words for Java which is a powerful Word automation API for working with popular Word document formats. You can either download Aspose.Words for Java or install it in your Maven-based application using the following configurations.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>20.1</version> <classifier>jdk17</classifier> </dependency>
Convert Word (DOC/DOCX) to PDF in Java
In order to convert a Word document to PDF, Aspose.Words for Java provides save() method of the Document class. You can simply load the Word document and call this method by specifying the output file name with the “.pdf” extension.
The following code sample shows how to convert a Word DOC to PDF in Java.
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 first N pages or a range of pages by specifying the starting page’s index.
The following code sample shows how to convert selected pages of Word DOC/DOCX to PDF in Java.
Convert Word DOC/DOCX to Particular PDF Standard in Java
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.
Convert Word to PDF with Text or Image Compression in Java
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.
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.
Try Aspose.Words for Java for Free
You can get a free temporary license to try and use Aspose.Words for Java.