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>3.1</version>
</dependency>You also need to provide a dependency to Docx4J:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-core</artifactId>
<version>11.5.8</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>11.5.8</version>
</dependency>Basic Usage
Here’s an example of how to use Office-stamper:
void main() {
// a java Bean, a record, or a map to use as context for the expressions found in the template.
var context = Map.of(
"company", Map.of("name", "Duff"),
"customer", Map.of("name", "Homer"),
"order", Map.of("id", "123", "date", "2023-01-01")
);
// 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
verron.pro