Skip to content

yezhi0/office-stamper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,869 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Office-stamper

Build Status Build Status Build Status Build Status

Introduction

Office-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.

Key Features

  • 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

Quick Start

class Example {
    static void main(String[] args) {
        // an object to use as context for the expressions found in the template.
        var context = new YourPojoContext(_, _ , _);

        // create a stamper that handles streams
        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);
        }
    }
}

If you already have a WordprocessingMLPackage (Docx4J document), you can use docxPackageStamper instead:

var stamper = OfficeStampers.docxPackageStamper(OfficeStamperConfigurations.standard());
WordprocessingMLPackage stamped = stamper.stamp(document, context);

Configuration Presets

Office-stamper provides three main configuration presets in OfficeStamperConfigurations to help you get started quickly:

  • minimal(): Sets up the base engine with placeholder preprocessors. Ideal for simple use cases.

  • standard(): Builds on minimal() by adding common comment processors (repeat, displayIf, replaceWith) and date/time formatting functions. This is the recommended starting point for most users.

  • full(): Further extends standard() with additional preprocessors for cleaning up language information and post-processors for cleaning up orphaned footnotes and endnotes.

You can create a configuration via one of these presets and then further customize it:

var configuration = OfficeStamperConfigurations.standard()
    .addResolver(new MyCustomResolver());

Maven Coordinates

<dependency>
    <groupId>pro.verron.office-stamper</groupId>
    <artifactId>engine</artifactId>
    <version>3.3</version>
</dependency>

<!-- You also need to provide a dependency to Docx4J -->
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-core</artifactId>
    <version>11.5.11</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
    <version>11.5.11</version>
</dependency>

Modules

This repository is structured in several Maven/Java modules:

  • Engine – core templating engine and public API

  • Utils – low-level utilities shared by modules

  • AsciiDoc – AsciiDoc to DOCX integration helpers

  • CLI – command line interface to stamp templates

Latest Release

The latest release is v3.3. See the Release Notes for details.

What’s new in 3.3 (high level)

  • SVG Support: Built-in support for inserting SVG images into documents.

  • Smart Tag Attribute Validation: New utility method to check attributes in DOCX smart tags.

  • Internal Cleanup: Simplified codebase by removing legacy utility classes.

  • Updated Dependencies: Latest Spring and Jackson versions for better security and performance.

What’s new in 3.2 (high level)

  • Expression API Refactor: Improved expression handling with the new Expression abstraction.

  • Table and Row Manipulation: New public APIs for easier document structure modification.

  • Comment Removal: Optional automatic cleanup of comments after processing.

  • Asciidoc Enhancements: Major improvements to Docx to Asciidoc conversion capabilities.

  • Updated Dependencies: Latest docx4j and Spring versions.

What’s new in 3.1 (high level)

  • API Consolidation: Continued streamlining of the public API by moving core interfaces like PlaceholderHooker, CommentHooker, and HookRemover to the pro.verron.officestamper.api package.

  • Internal Cleanup: Removed several internal utility classes and simplified the engine’s internal structure for better maintainability.

  • Experimental Features: Moved experimental stamper implementations to a dedicated pro.verron.officestamper.experimental package to clearly distinguish them from the stable API.

  • Logging: Refined logging levels; many verbose debug messages moved to trace level.

What’s new in 3.0 (high level)

  • Processing model overhaul: the engine now iterates hooks (placeholders, inline processors, comments) strictly in document order. See 3.0_UPDATE.md for the full rationale and migration guidance.

  • New hook concept: each placeholder/comment is treated as a hook — the iterator walks hooks one by one.

  • Hierarchical scopes: ContextTree resolve placeholders along the current branch; unresolved values bubble up to parent scopes.

  • Preprocessor-driven placeholders: include the placeholder preprocessor in your OfficeStamperConfiguration (already present in standard presets) to enable resolution.

  • Comment processor lifecycle simplified: processors are created per comment and executed once.

  • Postprocessors added: Postprocessors.removeTags(String) can remove all smart tags used by the engine after processing.

Breaking API highlights:

  • AbstractCommentProcessorCommentProcessor; introduce CommentProcessorFactory.

  • ContextDependent renamed to Hook (in pro.verron.officestamper.api).

  • Preprocessors moved: PlaceholderHooker and CommentHooker are now public in pro.verron.officestamper.api.

  • Postprocessors moved: HookRemover is now public in pro.verron.officestamper.api.

  • EvaluationContextConfigurerEvaluationContextFactory (fresh context per hook).

  • DocxDocument removed in favor of DocxPart.

  • New Insert API for multi-run insertions; PlaceholderReplacer removed.

  • OfficeStamper handles OpcPackage; use StreamStamper for stream-to-stream.

  • Expression parser is pluggable (ExpressionParser), SpEL by default.

  • DocxStamper#stamp now returns the document.

  • Java 23+ required; Maven release set to 25.

Minor updates:

  • Spring dependencies bumped to 7.0.2

  • New docs: engine/src/site/asciidoc/how-to-custom.adoc and HOW_DOES_IT_WORKS.adoc

  • Test matrix extended for Docx4J 11.5.7 and 11.5.8

  • New modules: asciidoc, utils

Sample Code

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. To view the resulting .docx documents, you can export the stamped documents with the method stamped.save(OutputStream)

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.

Contributing

Contributions are welcome! See the Contributing Guide for details on how to contribute to Office-stamper.

License

Office-stamper is released under the MIT License.

About

Easy-to-use template engine for creating docx documents in Java. Office-stamper automates generating Word, and in the future PowerPoint documents from templates with placeholders or instructions, which are processed via a Spring context to create personalized files easily.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Java 99.7%
  • Shell 0.3%