How to convert Word to PDF in Java

This quick tutorial is designed to provide brief information on how to convert Word to PDF in Java. You can perform this conversion of Word to PDF in Java by just loading the source DOCX file and saving it back as a PDF file. However, a number of options are available that can be used to configure the output PDF by setting different parameters and using them while saving the PDF file.

Steps to Convert Word to PDF in Java

  1. Configure your project to add Aspose.Words library from the Maven Repository for converting Word to PDF
  2. Load the input Word document using Document object to export to PDF
  3. Instantiate a PdfSaveOptions class object to create a custom PDF
  4. Set the properties of the output PDF file before conversion
  5. Save the Word file to create an output PDF file with the desired settings

You can use the simple step-by-step approach using Java code to convert DOCX to PDF by loading the source Word file into a Document class object. Later an instance of PdfSaveOptions is created that is used to set a variety of parameters, however this step is optional and can be skipped. In the last step, the loaded Word file is saved as PDF using the custom parameters.

Code to Convert Word to PDF in Java

In this tutorial while using Java Word to PDF converter is described. This code snippet uses a Document class object that can load any format of a Word file from a disk. The PdfSaveOptions is used for setting parameters like the set of pages that are to be rendered to PDF, mode of the file like the full screen when opened in any viewer or web browser, PDF compliance settings, rendering mode of 3D effects, Numeral format, pretty format, and many more. The final output PDF can be saved on disk or to a stream.

We have seen, how to convert DOC to PDF in Java. If you want to perform other types of conversions like Word to JPG, refer to the article how to convert Word to JPG in Java.

 English