Wednesday, 2 September 2020

[Java] How to convert Word to PDF using Spire.Doc for Java

Free Spire.Doc for Java is a free and professional Java Word API that enables Java applications to create, convert, manipulate and print Word documents without using Microsoft Office.

By using the method of doc.saveToFile(), Spire.Doc supports converting Word to other document formats, such as PDF, SVG, XML, XPS, Odt,Rtf and Html. In this article, I’ll give you an example of converting Word to PDF to show how to use this API efficiently. For more other functions, please download the API from the link and find examples in the Sample folder.

The test environment I used in this tutorial:

l  Intellij Idea2019.1

l  JDK 1.8.0

l  Spire.Doc.jar

Add Spire.Doc.jar to your project as a dependency

First of all, please register and log into the official website of e-iceblue, and then click the link to download Free Spire.Doc for Java to your local desk.


Next, unzip the package, open the “lib” folder and find Spire.Doc.jar.

Finally, add Spire.Doc.jar to your IDEA as a dependency.

The following is a sample Word document:

Converting Word to PDF directly

It only needs three rows of codes to converting Word to PDF with the same formats.

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

public class ToPDF {
   
public static void main(String[] args) {
       
//create a Document instance
       
Document document = new Document();
       
//Load a Word document
       
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

       
//save the document to a PDF file.
       
document.saveToFile("output/ToPDF.pdf", FileFormat.PDF);
    }
}

Output

Converting Word to PDF with password

The following are the steps to convert Word to PDF with password.

Step 1: Create a Document instance and load a sample Word document.

Step 2: Using the method ToPdfParameterList to create a parameter and set the password.

Step 3: Save the document to a PDF file.

import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.security.*;
public class ToPDFWithPassword {
   
public static void main(String[] args) {
       
//create a Document instance
       
Document document = new Document();
       
//Load a word document
       
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

       
//create a parameter
       
ToPdfParameterList toPdf = new ToPdfParameterList();         //set the password
       
String password = "Tinablog123";
        toPdf.getPdfSecurity().encrypt(password, password, PdfPermissionsFlags.
None, PdfEncryptionKeySize.Key_128_Bit);
       
//save the document to a PDF file
       
document.saveToFile("output/ToPDFWithPassword.pdf", toPdf);
    }
}

Output

Conclusion

This article showed you how to convert Word to PDF in Java using Free Spire.Doc for Java. If there is still question while testing codes mentioned above, please leave a note on our Forum.









No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...