Java Convert .docx File to .pdf File using XDocReport

Tags: docx pdf XDocReport

In this Java tutorial we learn how to convert a Word file to PDF file in Java using the XDocReport library.

Table of contents

  1. Add XDocReport Converter DOCX XWPF Dependency to Java Project
  2. How to convert .docx file to .pdf file in Java
  3. How to Use FileConverter Class to convert Word to PDF File

Add XDocReport Converter DOCX XWPF Dependency to Java Project

If you use Gradle build project, add the following dependency to the build.gradle file.

implementation group: 'fr.opensagres.xdocreport', name: 'fr.opensagres.xdocreport.converter.docx.xwpf', version: '2.0.3'

If you use Maven build project, add the following dependency to the pom.xml file.

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
    <version>2.0.3</version>
</dependency>

How to convert .docx file to .pdf file in Java

In Java, with a given Word file we can use the XDocReport API with the following steps to convert it to a PDF file.

  • Step 1: Open the .docx file as an InputStream using FileInputStream.
  • Step 2: Create new XWPFDocument object using the XWPFDocument(InputStream is) constructor.
  • Step 3: Create new instance of PdfOptions using the PdfOptions.create() static method.
  • Step 4: Write the .pdf file as an OutputStream using FileOutputStream.
  • Step 5: Use the PdfConverter.getInstance().convert( XWPFDocument document, OutputStream out, T options ) method to convert the .docx file to .pdf file.

In the FileConverter Java class below, we implement a method with the steps above to convert .docx file to .pdf file with given file names.

FileConverter.java

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

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

public class FileConverter {

    public void convertWordToPdf(String docxFileName, String pdfFileName) {
        try(InputStream inputStream = new FileInputStream(docxFileName);
            OutputStream outputStream = new FileOutputStream(pdfFileName)) {
            XWPFDocument document = new XWPFDocument(inputStream);
            PdfOptions options = PdfOptions.create();
            // Convert .docx file to .pdf file
            PdfConverter.getInstance().convert(document, outputStream, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How to Use FileConverter Class to convert Word to PDF File

For example, we have a sample Word file located at D:\SimpleSolution\Data\Document.docx with the content as the screenshot below.

Java Convert .docx File to .pdf File using XDocReport

In the following example Java program, we use the FileConverter class in the previous step to convert the sample Word file above to a PDF file.

ConvertDocxToPdfExample1.java

public class ConvertDocxToPdfExample1 {
    public static void main(String... args) {
        String docxFileName = "D:\\SimpleSolution\\Data\\Document.docx";
        String pdfFileName = "D:\\SimpleSolution\\Data\\Document.pdf";

        FileConverter fileConverter = new FileConverter();
        fileConverter.convertWordToPdf(docxFileName, pdfFileName);
    }
}

Execute the Java application, we have the PDF file be generated at D:\SimpleSolution\Data\Document.pdf as the screenshot below.

Java Convert .docx File to .pdf File using XDocReport

Happy Coding 😊

Java Convert .docx File to .html File using XDocReport

Java Export PDF File from HTML Template

Spring Boot Web Download PDF File from HTML Template

Spring Boot Generate PDF File from HTML Template