Aspose.Words is a high-level Word document processing API for performing various document management and manipulation tasks. The API supports generating, modifying, converting, rendering and printing documents without using Microsoft Word directly in the cross-platform application. also,
Aspose API supports popular file format processing and allows exporting or converting various types of documents to fixed layout file formats and most commonly used image/multimedia formats.
Aspose.words latest download (group: 761297826) https://www.evget.com/product/564/download
Word to PDF conversion is usually used before sharing documents. Various online Word to PDF converters are available that allow you to convert a single or a limited number of Word documents. However, with emerging MS Word automation and report generation solutions, automatic Word to PDF conversion has become an important part of the system. Also, batch conversion of DOC/DOCX to PDF needs to be done automatically to minimize time and effort. Keeping an eye out for situations like this, I'll show you how to programmatically automate the process of converting a Word DOC or DOCX document to PDF in Java.
Java Word to PDF Conversion Solution
You will learn about the following Word (DOC/DOCX) to PDF conversions in this article.
- Convert Word to PDF in Java
- Convert selected pages of Word DOC/DOCX to PDF in Java
- Convert Word to PDF using specific PDF standards such as PDF 1.5, PDF/A-1a, etc.
- Convert Word to PDF with image/text compression in Java
- Convert Word to PDF with Java Image Custom JPEG Quality
Java Word to PDF Conversion API
To convert Word DOCX/DOC to PDF, we will use Aspose.Words for Java, a powerful Word automation API for the popular word processing format. You can download Aspose.Words for Java or install it in your Maven based application using the following configuration.
repository:
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
rely:
<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
To convert a Word document to PDF, you simply load the Word document and save it with a ".pdf" extension. Following are the steps to convert DOCX/DOC to PDF in Java.
- Use the Document class to load a Word document.
- Save the document as a PDF using the Document.save() method.
The following code sample shows how to convert Word DOC to PDF in Java.
// Load the Word document from disk Document doc = new Document("word.docx"); // Save as PDF doc.save("output.pdf");
word document
PDF document
Convert selected pages of Word DOC/DOCX to PDF in Java
If you just want to convert Word's selected pages to PDF, you can use the PdfSaveOptions class to do it. You can convert the first N pages or a range of pages by specifying the index of the starting page.
Following are the steps to convert selected pages of Word DOCX/DOC to PDF in Java.
- Use the Document class to load a Word document.
- Create an instance of the PdfSaveOptions class.
- Set the index of the starting page and the number of pages to convert.
- Save the Word document as a PDF using the Document.save() method.
The following code sample shows how to convert selected pages of Word DOC/DOCX to PDF in Java.
// Load the Word document from disk Document doc = new Document("word.docx"); PdfSaveOptions options = new PdfSaveOptions(); // Convert 3 pages starting from index 1 where 0 is the first page's index options.setPageIndex(1); options.setPageCount(3); // Save Word as PDF doc.save("output.pdf", options);
Convert Word DOC/DOCX to a specific PDF standard in Java
PDF documents may conform to various PDF standards, such as PDF/A-1a, PDF 1.5, and so on. If you want to convert a 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 using specific PDF standards in Java.
// Load the Word document from disk Document doc = new Document("word.docx"); // Set PDFSaveOption compliance to PDF15 PdfSaveOptions options = new PdfSaveOptions(); options.setCompliance(PdfCompliance.PDF_15); // Convert Word to PDF doc.save("output.pdf", options);
Convert Word to PDF with Text or Image Compression in Java
You can also reduce the size of the resulting PDF document by compressing the text or images 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 the following text compression options for the PdfTextCompression class:
- None: No text compression.
- Flate: Flate (ZIP) compression.
Image Compression
Image compression options are available in the PdfImageCompression class.
- Auto: Automatically selects the most appropriate compression for each image.
- Jpeg: The image is converted to JPEG format (transparency not supported).
The following code sample shows how to convert Word DOCX to PDF by applying text and image compression in Java
// Load the Word document from disk Document doc = new Document("word.docx"); PdfSaveOptions options = new PdfSaveOptions(); // Text and image compression options.setTextCompression(PdfTextCompression.FLATE); options.setImageCompression(PdfImageCompression.AUTO); // Save Word as PDF doc.save("output.pdf", options);
Convert Word to JPEG-quality PDF in Java
You can also customize and control JPEG quality in Word to PDF conversion. PdfSaveOptions.setJpegQuality is used to set JPEG quality, it can be from 0 (worst quality for maximum compression) to 100 (best quality for minimum compression).
The following code sample shows how to specify JPEG quality when converting Word DOCX to PDF in Java.
// Load the Word document from disk Document doc = new Document("word.docx"); // Set Jpeg quality PdfSaveOptions options = new PdfSaveOptions(); options.setJpegQuality(100); // Convert Word to PDF doc.save("output.pdf", options);
The above is how to convert Word DOC/DOCX to PDF in Java. If you have other questions about the product, please feel free to consult us, or join our official technical exchange group