Getting Started with Office-stamper
This guide helps you get started with Office-stamper, from adding the dependency to your project to creating your first document.
Adding Office-stamper to Your Project
Maven
Add the following dependency to your pom.xml
:
<dependency>
<groupId>pro.verron.office-stamper</groupId>
<artifactId>engine</artifactId>
<version>2.7.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>
Basic Usage
Here’s an example of how to use Office-stamper:
class Example {
public static void main(String[] args) {
// a java object to use as context for the expressions found in the template.
var context = new YourPojoContext(_, _ , _);
// an instance of the stamper
var stamper = OfficeStampers.docxStamper();
try(
// Path to the .docx template file
var template = Files.newInputStream(Paths.get("your/docx/template/file.docx"));
// Path to write the resulting .docx document
var output = Files.newOutputStream(Paths.get("your/desired/output/path.docx"))
) {
stamper.stamp(template, context, output);
}
}
}
Creating a Template
- Open Microsoft Word or any other word processor that can save in .docx format.
- Create your document with placeholders for dynamic content
- Use expressions like
${person.name}
for natural replacements. - Add comments with special instructions for more advanced features
- Save the document as a .docx file
Next Steps
- Learn more about Template Expressions
- Explore Comment Processors for advanced templating
- See Custom Settings for configuration options