Office-stamper (formerly docx-stamper) is a Java template engine that allows for dynamic creation of DOCX documents at runtime. You design a template using your preferred Word processor; and office-stamper will generate documents based on that template.
-
Expression-based templating: Use Spring Expression Language (SpEL) for powerful template expressions
-
Comment-based processing: Add special instructions as comments in your Word documents
-
Formatting preservation: All formatting from the original template is preserved
-
Custom functions: Extend the templating capabilities with your own functions
-
Type-safe: Strong typing for Java integration
-
Flexible configuration: Customize the behavior to suit your needs
class Example {
public static void main(String[] args) {
// an object to use as context for the expressions found in the template.
var context = new YourPojoContext(_, _ , _);
var stamper = OfficeStampers.docxStamper();
var templatePath = Paths.get("your/docx/template/file.docx");
var outputPath = Paths.get("your/desired/output/path.docx");
try(
var template = Files.newInputStream(templatePath);
var output = Files.newOutputStream(outputPath);
) {
stamper.stamp(template, context, output);
}
}
}
<dependency>
<groupId>pro.verron.office-stamper</groupId>
<artifactId>engine</artifactId>
<version>2.8.0</version>
</dependency>
<!-- You also need to provide a dependency to Docx4J -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-core</artifactId>
<version>11.5.3</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>11.5.3</version>
</dependency>
Comprehensive documentation is available at verronpro.github.io/office-stamper.
The latest release is v2.8.0. See the Release Notes for details.
The source code contains a set of tests showing how to use the features.
If you want to run them yourself, clone the repository and run mvn test
with the system property -DkeepOutputFile=true
so that the resulting .docx documents will not be cleaned up and let you view them.
If you want to have a look at the .docx templates used in the tests, have a look at the sources subfolder in the test folder.
Contributions are welcome! See the Contributing Guide for details on how to contribute to Office-stamper.