Java Convert Word File Docx to PDF using documents4j library

Tags: documents4j Java docx to pdf Convert docx to pdf

In this Java tutorial we learn how to use the documents4j Java library to convert Microsoft Word .docx file into .pdf file in Java application.

Table of contents

  1. Add documents4j dependencies to Java project
  2. Convert Word file Docx to PDF File in Java
  3. Implement PdfConverter re-use class

Add documents4j dependencies to Java project

To use the documents4j library in the Gradle build project, add the following documents4j dependencies into the build.gradle file.

implementation 'com.documents4j:documents4j-local:1.1.7'
implementation 'com.documents4j:documents4j-transformer-msoffice-word:1.1.7'

To use the documents4j library in the Maven build project, add the following documents4j dependencies into the pom.xml file.

<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.1.7</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.1.7</version>
</dependency>

To have more information about the documents4j Java library you can visit the project home page at documents4j.com

Convert Word file Docx to PDF File in Java

For example we have an Microsoft Word file located at D:\SimpleSolution\Data\Welcome to Word.docx

Java Convert Word File Docx to PDF using documents4j library

With file content open in Microsoft Word as below.

Java Convert Word File Docx to PDF using documents4j library

In the following program we use the documents4f LocalConverter class to convert the above file to .pdf file at D:\SimpleSolution\Data\Welcome to Word.pdf

ConvertWordToPdfExample1.java

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

import java.io.File;

public class ConvertWordToPdfExample1 {
    public static void main(String... args) {
        File wordFile = new File("D:\\SimpleSolution\\Data\\Welcome to Word.docx");
        File pdfFile = new File("D:\\SimpleSolution\\Data\\Welcome to Word.pdf");

        IConverter converter = LocalConverter.builder().build();

        converter.convert(wordFile).as(DocumentType.MS_WORD)
                .to(pdfFile).as(DocumentType.PDF)
                .execute();

        converter.shutDown();
    }
}

Execute the program above we have the .pdf file be generated in the folder as below.

Java Convert Word File Docx to PDF using documents4j library

Open the created .pdf file with a PDF reader we have the file content as below screenshot.

Java Convert Word File Docx to PDF using documents4j library

Below is another Java example also convert .docx to .pdf file with InputStream and OutputStream.

ConvertWordToPdfExample2.java

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ConvertWordToPdfExample2 {
    public static void main(String... args) {

        String docxFileName = "D:\\SimpleSolution\\Data\\Welcome to Word.docx";
        String pdfFileName = "D:\\SimpleSolution\\Data\\Welcome to Word.pdf";

        try (InputStream docxInputStream = new FileInputStream(docxFileName);
             OutputStream pdfOutputStream = new FileOutputStream(pdfFileName)) {

            IConverter converter = LocalConverter.builder().build();

            converter.convert(docxInputStream).as(DocumentType.MS_WORD)
                    .to(pdfOutputStream).as(DocumentType.PDF)
                    .execute();

            converter.shutDown();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Execute the above application we can get the same result with previous Java program.

Implement PdfConverter re-use class

At this step we implement a new Java class named PdfConverter for re-use later in your Java program.

PdfConverter.java

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class PdfConverter {

    public void convertWordToPdf(String wordFileName, String pdfFileName) throws IOException {
        try (InputStream docxInputStream = new FileInputStream(wordFileName);
             OutputStream pdfOutputStream = new FileOutputStream(pdfFileName)) {

            IConverter converter = LocalConverter.builder().build();

            converter.convert(docxInputStream).as(DocumentType.MS_WORD)
                    .to(pdfOutputStream).as(DocumentType.PDF)
                    .execute();

            converter.shutDown();
        }
    }
}

In the following Java program we use the PdfConverter class above to convert docx file to pdf file.

ConvertWordToPdfExample3.java

import java.io.IOException;

public class ConvertWordToPdfExample3 {
    public static void main(String... args) {
        String docxFileName = "D:\\SimpleSolution\\Data\\Welcome to Word.docx";
        String pdfFileName = "D:\\SimpleSolution\\Data\\Welcome to Word.pdf";

        try {
            PdfConverter pdfConverter = new PdfConverter();
            pdfConverter.convertWordToPdf(docxFileName, pdfFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Happy Coding 😊