[](https://travis-ci.org/thombergs/docx-stamper)
npm install docx-stamper-master
docx-stamper is a Java template engine for docx documents. You create a template .docx document with your favorite word processor
and feed it to a DocxStamper instance to create a document based on the template at runtime. Example code:
``java`
MyContext context = ...; // your own POJO against which expressions found in the template
// will be resolved
InputStream template = ...; // InputStream to your .docx template file
OutputStream out = ...; // OutputStream in which to write the resulting .docx document
DocxStamper stamper = new DocxStamperConfiguration()
.build();
stamper.stamp(template, context, out);
out.close();
or ${person.name.equals("Homer") ? "Duff" : "Budweiser"} in the text of your .docx template and provide a context object against which the expression can be resolved. docx-stamper will try to keep the original formatting of the text in the template intact. You can use the full feature set of Spring Expression Language (SpEL).The value an expression resolves to may be of the following types:
| Type of expression value | Effect |
| ---|---|
| java.lang.Object | The expression is replaced by the String representation of the object (
String.valueOf()).
| java.lang.String | The expression is replaced with the String value.|
| java.util.Date | The expression is replaced by a formatted Date string (by default "dd.MM.yyyy"). You can change the format string by registering your own DateResolver.|
| org.wickedsource.docxstamper...Image | The expression is replaced with an inline image.|If an expression cannot be resolved successfully, it will be skipped (meaning the expression stays in the document as it was in the template). To support more than the above types you can implement your own TypeResolver. To register your own TypeResolver with docx-stamper, use the following code:
`java
ITypeResolver typeResolver = ...; // instance of your own ITypeResolver implementation
Class> type ...; // class of expression values your resolver handles
DocxStamper stamper = new DocxStamperConfiguration()
.addTypeResolver(type, typeResolver)
.build();
`Customizing the SpEL Evaluation Context
If you want to take more control over the evaluation of expressions, you can implement a EvaluationContextConfigurer
and customize Springs
StandardEvaluationContext to your needs. You can register an EvaluationContextConfigurer like this:`java
EvaluationContextConfigurer configurer = ...;
DocxStamper stamper = new DocxStamperConfiguration()
.setEvaluationContextConfigurer(configurer)
.build();
`Adding custom functions to the Expression Language
If you want to create custom functions (for different number formats or different date formats, for example), you can register functions
which can then be used in the expression language. The following code for example adds a function
toUppercase(String)
which can be used within the .docx document to uppercase a String:`java
DocxStamper stamper = new DocxStamperConfiguration()
.exposeInterfaceToExpressionLanguage(UppercaseFunction.class, new UppercaseFunctionImpl());
.build();public interface UppercaseFunction {
String toUppercase(String string);
}
public static class UppercaseFunctionImpl implements UppercaseFunction {
@Override
public String toUppercase(String string) {
return string.toUpperCase();
}
}
`
Conditional Display and Repeating of Elements
Besides replacing expressions, docx-stamper can process comments on paragraphs of text in the template .docx document and do manipulations on the template based on these comments. By default, you can use the following expressions in comments:| Expression in .docx comment | Effect |
| --------------------------------- |---------|
|
displayParagraphIf(boolean) | The commented paragraph is only displayed in the resulting .docx document if the boolean condition resolves to true.|
| displayTableRowIf(boolean) | The table row surrounding the commented paragraph is only displayed in the resulting .docx document if the boolean condition resolves to true.|
| displayTableIf(boolean) | The whole table surrounding the commented paragraph is only displayed in the resulting .docx document if the boolean condition resolves to true.|
| repeatTableRow(List