SoFunction
Updated on 2025-04-06

Java implements one-click conversion of Word documents to PDF

During development, you often encounter the need to convert Word documents into PDF format, such as generating reports, contracts, etc. There are many useful libraries in Java that can implement this function. Here are two common methods, using Apache POI and Docx4J combined with iText library to implement Word to PDF.

Method 1: Use Apache POI and iText

1. Introduce dependencies

If you use Maven to manage your project, add the following dependencies in:

<dependencies>
    <!-- Apache POI deal with Word document -->
    <dependency>
        <groupId></groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.3</version>
    </dependency>
    <!-- iText generate PDF document -->
    <dependency>
        <groupId></groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.13.3</version>
    </dependency>
</dependencies>

2. Code example

import .*;
import ;
import ;
import ;
import ;
 
import .*;
 
public class WordToPdfWithPOI {
    public static void main(String[] args) {
        try {
            // Read Word documents            FileInputStream fis = new FileInputStream("");
            XWPFDocument document = new XWPFDocument(fis);
 
            // Create PDF document            Document pdfDoc = new Document();
            (pdfDoc, new FileOutputStream(""));
            ();
 
            // traverse paragraphs of Word document            for (XWPFParagraph paragraph : ()) {
                StringBuilder text = new StringBuilder();
                // traverse the text running object in the paragraph                for (XWPFRun run : ()) {
                    ((0));
                }
                // Add paragraph text to PDF document                Paragraph pdfParagraph = new Paragraph(());
                (pdfParagraph);
            }
 
            // Close documents and streams            ();
            ();
            ();
 
            ("Word to PDF successfully!");
        } catch (Exception e) {
            ();
            ("Word to PDF failed:" + ());
        }
    }
}

3. Code explanation

Read Word documents: Use FileInputStream to read the file, and then use the XWPFDocument class to load it into memory.

Create a PDF document: Create a Document object to represent a PDF document, associate the output stream with PdfWriter, and then open the document to prepare for writing.

Traversing Word document paragraphs: Traversing each paragraph of a Word document, extracting the text in the paragraph, adding it to the StringBuilder, and then creating a Paragraph object to add it to the PDF document.

Close Documents and Streams: After the operation is complete, close PDF documents, Word documents, and input streams.

Method 2: Use Docx4J

1. Introduce dependencies

Add the following dependencies in :

<dependencies>
    <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j-JAXB-Internal</artifactId>
        <version>11.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
        <version>11.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j</artifactId>
        <version>11.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j-export-fo</artifactId>
        <version>11.4.9</version>
    </dependency>
</dependencies>

2. Code example

import org.docx4j.Docx4J;
import org.;
import org.;
 
import ;
import ;
import ;
 
public class WordToPdfWithDocx4J {
    public static void main(String[] args) {
        try {
            // Load Word documents            WordprocessingMLPackage wordMLPackage = (new File(""));
 
            // Create FOSettings object            FOSettings foSettings = ();
            (wordMLPackage);
 
            // Create output stream            OutputStream os = new FileOutputStream(new File(""));
 
            // Convert and save as PDF            (foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
 
            // Turn off the output stream            ();
 
            ("Word to PDF successfully!");
        } catch (Exception e) {
            ();
            ("Word to PDF failed:" + ());
        }
    }
}

3. Code explanation

Load Word documents: Load the file using the method.

Create FOSettings object: FOSettings is used to configure conversion settings and set the loaded Word document.

Create Output Stream: Create FileOutputStream to output PDF files.

Convert and save as PDF: Call the method for conversion and save the result to the output stream.

Close the output stream: After the operation is completed, close the output stream.

Hey, friends! Both methods can help you convert Word documents into PDFs in Java. You can choose the right method according to your needs and preferences.

This is the article about Java implementing one-click conversion of Word documents into PDF. For more related Java Word to PDF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!