SoFunction
Updated on 2025-04-14

Java uses docx4j+Freemarker to generate word documents

Technical Solution

java 1.8 + docx4j + Freemarker

maven dependency

<dependency>
    <groupId></groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version> <!-- Please use the latest version -->
</dependency>

<!-- /artifact/org.docx4j/docx4j -->
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>6.1.2</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>6.1.0</version>
</dependency>
<!-- slf4j for logging -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.36</version>
</dependency>

Create a template file

Save to src/main/resources/templates/ with file name

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
    <title>${name}Resume</title>
    <style>
        @page {
            size: A4;
            margin: 5mm 10mm; /* The margins in the upper and lower and left and right directions are 10mm and 20mm respectively */
        }
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
        }
        .container {
            width: 80%;
            margin: 0 auto;
            padding: 20px;
            background-color: #ffffff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .section-title {
            color: #4CAF50;
            margin-top: 20px;
        }
        .section-content {
            margin: 10px 0;
        }
        .contact-info, .skills, .experience, .education {
            margin-bottom: 20px;
        }
        .experience, .education {
            margin-top: 10px;
        }
        ul {
            list-style-type: none;
            padding-left: 0;
        }
        li {
            margin-bottom: 10px;
        }
        .job, .degree {
            font-weight: bold;
        }
    </style>
</head>



<body>

<div class="container">
    <h1>${name}Resume</h1>

    <!-- contact information -->
    <div class="contact-info">
        <h2 class="section-title">contact information</h2>
        <p>Mail: ${email}</p>
        <p>Telephone: ${phone}</p>
    </div>

    <!-- Work experience -->
    <div class="experience">
        <h2 class="section-title">Work experience</h2>
        <#list experience as job>
        <div class="job">
            <p><strong>${}</strong> exist ${}(${} - ${})</p>
            <p>${}</p>
        </div>
        </#list>
    </div>

    <!-- Educational background -->
    <div class="education">
        <h2 class="section-title">Educational background</h2>
        <#list education as degree>
            <div class="degree">
                <p><strong>${}</strong> ${}(${}Year)</p>
            </div>
        </#list>
    </div>
</div>

</body>
</html>

Implement code

package ;

import ;
import ;
import ;
import org.docx4j.Docx4J;
import org.;
import org..Docx4JException;
import org.;
import org.;
import org.;

import ;
import ;
import ;
import ;

/**
  * @Program: RuoYi-master
  * @ClassName: GenerateWord
  * @author: zhouzihao
  * @date: February 20, 2025, 0020 at 03:25 pm
  * @version: 1.0.0
  * @Description:
  * @Time: 2025-02-20 15:25
  */
public class GenerateWordUtil {
    // Template path    private static final String templatesPath = "/templates";
    // Template file    private static final String templatesFile = "";

    public static void generateWord(Map<String, Object> data, String filePName) {
        // Configure Freemarker        Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);
        (, templatesPath);

        // Get HTML template        try {
            Template template = (templatesFile);

            // Use Freemarker to fill the template            StringWriter writer = new StringWriter();
            (data, writer);
            String htmlContent = ();

            // Create Word document package            WordprocessingMLPackage wordMLPackage = ();
            MainDocumentPart mainDocumentPart = ();

            // Create an XHTML importer implementation class instance            XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);

            // Import HTML content into Word documents            <Object> convertedXHTML = (htmlContent, null);

            // Add the converted content to the body of the document            ().addAll(convertedXHTML);

            // Save the generated Word document            File outputFile = new File(filePName);
            (wordMLPackage, outputFile, Docx4J.FLAG_NONE);
            ("Word document generation successfully:" + ());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        } catch (InvalidFormatException e) {
            throw new RuntimeException(e);
        } catch (Docx4JException e) {
            throw new RuntimeException(e);
        }

    }

}

This is the article about Java using docx4j+Freemarker to generate word documents. For more related Java generation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!