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.
- Convert Word 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 to PDF Conversion API
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> <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, 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.
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.
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.
In this article, you have learned how to convert Word DOCX or DOC to PDF using Java. Furthermore, you have seen how to customize the Word to PDF conversion in different scenarios. You can explore more about Aspose.Words for Java using the documentation.
- Word DOC/DOCX to PDF in C#
- PDF to Excel in C#
- PDF to MS Word Documents in Java
- Word to Markdown in Java
- Word to EPUB Conversion in Java
- HTML to Word Conversion in Java
- TXT Files to PDF in Java
- PDF File to Text in Python
- XLSX to JSON in Java
- Excel to JSON in Java
- DOCX to JSON in Java
- DOC to JSON in Java
- Word to JSON in Java
- TXT to JSON in Java
- JSON to PDF in Python
- Excel to JSON in Java
- TXT to JSON in Python
- PNG to Word in C# .NET
- JPG to Word in C# .NET
- Image to Word in C#
- Word to HTML in C#
- Word DOCX to Markdown in Java
- Extract Images from Word DOC in Java
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.