Java - Convert Word Doc or Docx to PDF

Method 2: If you are not using maven, you can download the JAR file from this link, extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.

Convert Word to PDF in Java

Convert Word to PDF is extremely easy with Spire.Doc for Java. You just need to follow the two steps below:

import com.spire.doc.Document; import com.spire.doc.FileFormat; public class ConvertWordToPdf  public static void main(String[] args) //Create a Document instance and load a Word document Document doc = new Document("Sample.docx"); //Save the document to PDF doc.saveToFile("ToPdf.pdf", FileFormat.PDF); > > 
Enter fullscreen mode

Exit fullscreen mode

Convert Word to PDF in Java

Convert Word to PDF/A in Java

PDF/A is a special PDF format designed for long-term preservation of electronic documents. Spire.Doc for Java supports converting Word to the following PDF/A documents:

The following are the steps to Convert a Word document to a PDF/A document:

import com.spire.doc.Document; import com.spire.doc.ToPdfParameterList; import com.spire.pdf.PdfConformanceLevel; public class ConvertWordToPdfA  public static void main(String[] args) //Create a Document instance and load a Word document Document doc = new Document("Sample.docx"); //Create a ToPdfParameterList instance ToPdfParameterList parameterList = new ToPdfParameterList(); //Set the conformance level of the PDF parameterList.setPdfConformanceLevel(PdfConformanceLevel.Pdf_A_1_A); //Save the document to PDF/A doc.saveToFile("ToPdfA.pdf", parameterList); > > 
Enter fullscreen mode

Exit fullscreen mode

Convert Word to PDF/A in Java

Convert Word to Password Protected PDF in Java

You can also encrypt the PDF with password during the Word to PDF conversion. The following are the steps to do so.

import com.spire.doc.Document; import com.spire.doc.ToPdfParameterList; import com.spire.pdf.security.PdfEncryptionKeySize; import com.spire.pdf.security.PdfPermissionsFlags; public class ConvertWordToPasswordProtectedPDF  public static void main(String[] args) //Create a Document instance and load a Word document Document doc = new Document("Sample.docx"); //Create a ToPdfParameterList instance ToPdfParameterList toPdf = new ToPdfParameterList(); //Set open password and permission password for PDF String password = "password"; toPdf.getPdfSecurity().encrypt(password, password, PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_128_Bit); //Save the Word document to PDF with password doc.saveToFile("ToPdfWithPassword.pdf", toPdf); > > 
Enter fullscreen mode

Exit fullscreen mode

Convert Word to Password Protected PDF in Java

Conclusion

This article introduces how to convert Word to PDF using the Document.saveToFile() method provided by Spire.Doc for Java. In addition to PDF, you can also use the Document.saveToFile() method to convert Word document to other file formats like Rtf, Html, Odt, Txt, Epub, PostScript, Xml, Svg, XPS and more.