
docx-stamper
Open Source Java Library for DOCX Template Processing
Generate Word Documents by Replacing Expressions and Processing Comments in .docx Files via Open Source Java API.
What is docx-stamper?
docx-stamper is an open-source Java library that enables developers to programmatically generate and customize Word documents by stamping dynamic content into .docx templates. It leverages Spring Expression Language (SpEL) to evaluate expressions embedded in the document text and supports conditional display, repetition, and image insertion via comments. The library preserves original formatting and integrates seamlessly with existing Java applications.
Designed for simplicity and flexibility, docx-stamper allows developers to define templates in Microsoft Word and inject data at runtime using POJOs. It supports expression replacement, table row repetition, paragraph visibility control, and custom type resolvers or comment processors. With its lightweight architecture and extensible API, docx-stamper is ideal for generating invoices, reports, certificates, and other document types requiring dynamic content.
Getting Started with docx-stamper
docx-stamper is available via Maven from JCenter. Add the repository and dependency to your pom.xml. You must also include a compatible version of docx4j yourself (e.g., docx4j-JAXB-ReferenceImpl). For the latest version and source code, visit the [docx-stamper GitHub](https://github.com/thombergs/docx-stamper) repository. The library is lightweight and integrates easily into existing Java projects.
Install docx-stamper via Maven
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependency>
<groupId>org.wickedsource.docx-stamper</groupId>
<artifactId>docx-stamper</artifactId>
<version>1.5.0</version>
</dependency>Replace Expressions in DOCX Templates
docx-stamper enables dynamic content injection by replacing expressions like `${person.name}` in the template text using Spring SpEL. It supports strings, objects, dates, and images, preserving original formatting. You can customize date formatting or add custom resolvers for additional types. If an expression fails to resolve, it remains unchanged unless configured otherwise. This makes it ideal for generating personalized documents with minimal code.
How to Replace Expressions in a DOCX Template via Java?
MyContext context = new MyContext();
context.setPerson(new Person("Alice"));
InputStream template = new FileInputStream("template.docx");
OutputStream out = new FileOutputStream("output.docx");
DocxStamper stamper = new DocxStamperConfiguration().build();
stamper.stamp(template, context, out);
out.close();Conditional Display & Repetition via Comments
docx-stamper supports advanced template control using comments in the .docx file. You can conditionally show/hide paragraphs, repeat table rows or document parts, and replace words dynamically using expressions like `displayParagraphIf(boolean)` or `repeatTableRow(List)`. This allows building complex layouts such as invoices with variable line items or reports with conditional sections—all without modifying the Java code.
How to Repeat Table Rows Based on a List via Java?
List orders = Arrays.asList(
new Order("Item A", 2),
new Order("Item B", 5)
);
context.setOrders(orders);
// In template: comment on table row: repeatTableRow(${orders})
stamper.stamp(template, context, out); Custom Type Resolvers & Functions
docx-stamper allows extending supported expression types via custom `ITypeResolver` implementations. You can also register custom functions for SpEL, such as formatting numbers or dates differently. This flexibility lets you handle domain-specific logic directly in templates—like `${toUppercase(customer.name)}`—without cluttering your Java code, enabling cleaner and more expressive templates.
How to Add a Custom SpEL Function via Java?
DocxStamper stamper = new DocxStamperConfiguration()
.exposeInterfaceToExpressionLanguage(
UppercaseFunction.class,
new UppercaseFunctionImpl()
).build();
public interface UppercaseFunction {
String toUppercase(String s);
}
public static class UppercaseFunctionImpl implements UppercaseFunction {
public String toUppercase(String s) { return s.toUpperCase(); }
}Error Handling & Evaluation Context Customization
docx-stamper provides robust error handling: by default, unresolved expressions throw exceptions, but you can disable this with `setFailOnUnresolvedExpression(false)`. You can also fully customize the SpEL evaluation context via `EvaluationContextConfigurer`, enabling custom variable resolution, security settings, or performance tuning. This ensures safe and predictable behavior in production environments.
How to Configure Error Handling via Java?
DocxStamper stamper = new DocxStamperConfiguration()
.setFailOnUnresolvedExpression(false)
.build();
// Now unresolved expressions are silently skipped