Convert Word to PDF in Java

This article demonstrates how to use the Document.saveToFile() method offered by Spire.Doc for Java to convert a Word document to PDF. By using this method, you can also convert Word documents to XPS, SVG, XML, RTF, etc. Furthermore, the ToPdfParameterList class is introduced to control how Word documents will be converted to PDF. For example, whether to compress image quality or not while converting.

Below is a screenshot of the input Word document.

Convert Word to PDF in Java

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ToPdfParameterList;

public class WordToPDF {
    public static void main(String[] args)  {

        //Create Document object
        Document doc = new Document();

        //Load the file from disk.
        doc.loadFromFile("input.docx");

        //Create an instance of ToPdfParameterList.
        ToPdfParameterList ppl=new ToPdfParameterList();

        //Embeds full fonts by default when IsEmbeddedAllFonts is set to true.
        ppl.isEmbeddedAllFonts(true);

        //Set setDisableLink to true to remove the hyperlink effect for the result PDF page.
        //Set setDisableLink to false to preserve the hyperlink effect for the result PDF page.
        ppl.setDisableLink(true);

        //Set the output image quality as 40% of the original image. 80% is the default setting.
        doc.setJPEGQuality(40);

        //Save to file.
        doc.saveToFile("ToPDF.pdf", ppl);
    }
}

Convert Word to PDF in Java