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

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.PageSet;
import com.aspose.words.PdfCompliance;
import com.aspose.words.PdfPageMode;
import com.aspose.words.PdfSaveOptions;
public class ConvertWordToPdfInJava {
public static void main(String[] args) throws Exception {// Word to PDF Java code main function
// Create a license object to avoid limitations of the trial version while converting Word to PDF
License licWordToPdf = new License();
licWordToPdf.setLicense("Aspose.Words.lic");
// Load the sample Word file to be converted to PDF
Document inputDocx = new Document("InputWordDocxForConversionToPDF.docx");
// Create and initialize the PdfSaveOptions object for setting parameters for output PDF
PdfSaveOptions OutputPdfOptions = new PdfSaveOptions();
// Set page set to print page 1 and 2 only to the PDF
OutputPdfOptions.setPageSet(new PageSet(0,1));
// Set the Full Screen mode while opening the PDF document in browser
OutputPdfOptions.setPageMode(PdfPageMode.FULL_SCREEN);
// Set the PDF compliance value for the output PDF
OutputPdfOptions.setCompliance(PdfCompliance.PDF_17);
// Save the PDF with the desired configuration
inputDocx.save("WordToPdfJava.pdf", OutputPdfOptions);
}
}

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