
docstencil-core
Open Source Java/Kotlin Library for Word Document Generation
Create Professional DOCX Files with Mail Merge, Loops, Conditionals & Formatting via Template-Based API.
What is docstencil-core?
docstencil-core is an open-source Java and Kotlin library for generating professional DOCX documents using familiar Word templates. It implements a template-based approach where developers design layouts in Microsoft Word using simple placeholder syntax like {name}, {for}, and {if}. This eliminates the need for complex XML manipulation or low-level document APIs. The library supports nested data access, built-in formatting functions, and parallel evaluation—ideal for dynamic content generation such as invoices, contracts, and reports.
Designed for simplicity and developer productivity, docstencil-core requires only three lines of code to render a document. It integrates seamlessly with Spring Boot and works with plain Java/Kotlin objects, records, and data classes. With minimal dependencies (only kotlin-reflect), it avoids the heavy footprint of alternatives like Apache POI or docx4j. Templates remain editable by non-developers using standard Word, making collaboration between technical and business teams effortless. Its expressive templating language supports conditionals, loops, dot notation, and custom functions for robust document automation.
Getting Started with docstencil-core
docstencil-core is available on Maven Central and requires JDK 8 or higher. Install via Maven or Gradle using the official artifact com.docstencil:docstencil-core. The library has minimal dependencies (only kotlin-reflect) and integrates seamlessly with Spring Boot or plain Java/Kotlin projects. After installation, create a Word template with placeholders like {name} and {for}, then render it in just three lines of code. For full examples and documentation, visit the docstencil-core GitHub.
Install docstencil-core via Maven & Gradle
<dependency>
<groupId>com.docstencil</groupId>
<artifactId>docstencil-core</artifactId>
<version>0.2.1</version>
</dependency>implementation("com.docstencil:docstencil-core:0.2.1")Template-Based Document Generation
docstencil-core uses a template-first approach: design your document layout in Microsoft Word using intuitive placeholders like {name}, {customer.address.city}, and {for item in items}. This eliminates the need for XML manipulation or complex APIs. Templates remain editable by non-developers using standard Word, while developers focus solely on data binding. The library supports nested data access via dot notation, enabling clean integration with POJOs, records, and Kotlin data classes. With parallel evaluation support, large templates with lazy-loaded data benefit from improved performance. This approach drastically reduces development time and improves maintainability compared to traditional document generation libraries.
How to Generate a DOCX File from a Template via Java?
import com.docstencil.core.api.OfficeTemplate;
import java.util.Map;
public class Main {
public static void main(String[] args) {
var template = OfficeTemplate.fromFile("template.docx");
var result = template.render(Map.of("name", "World"));
result.writeToFile("Output.docx");
}
}Loops & Conditionals for Dynamic Content
docstencil-core provides powerful templating constructs to generate dynamic tables, lists, and conditional sections. Use {for} loops to iterate over collections and generate repeating content like invoice line items or report sections. Conditionals with {if} and {end} allow you to show or hide content based on data values—ideal for legal clauses, discounts, or user-specific messaging. The syntax is intuitive and closely mirrors natural language, reducing the learning curve. Combined with dot notation, you can access deeply nested structures and apply logic directly in the template. This makes it easy to build complex, data-driven documents without writing boilerplate code.
How to Generate a Dynamic Table with Loops via Kotlin?
import com.docstencil.core.api.OfficeTemplate
fun main() {
val template = OfficeTemplate.fromFile("invoice.docx")
val items = listOf(
mapOf("product" to "Widget", "qty" to 5, "price" to 9.99),
mapOf("product" to "Gadget", "qty" to 3, "price" to 14.99)
)
val result = template.render(mapOf("items" to items))
result.writeToFile("Invoice.docx")
}Built-in Formatting Functions
docstencil-core includes a rich set of built-in functions for formatting dates, numbers, and strings directly in templates. Use syntax like {$format(date, "MMMM dd, yyyy")} to ensure consistent presentation without cluttering your business logic. Functions support pipe notation and lambdas for chaining operations, enabling expressive and readable templates. This eliminates the need for custom formatting utilities and reduces the risk of inconsistencies across generated documents. Whether you're generating financial reports, invoices, or legal contracts, precise formatting is handled declaratively within the template itself.
How to Format Dates and Numbers in Templates via Java?
import com.docstencil.core.api.OfficeTemplate;
import java.time.LocalDate;
import java.util.Map;
public class Main {
public static void main(String[] args) {
var template = OfficeTemplate.fromFile("report.docx");
var data = Map.of(
"date", LocalDate.now(),
"amount", 1234.56
);
var result = template.render(data);
result.writeToFile("FormattedReport.docx");
}
}Spring Boot Integration
docstencil-core is framework-agnostic and integrates effortlessly with Spring Boot. Add the dependency to your project and inject OfficeTemplate into any Spring component. Use dependency injection to fetch data from services and render templates within REST endpoints or scheduled jobs. Since the library has no Spring dependencies, it remains lightweight and portable. This makes it ideal for microservices architectures where document generation is needed on-demand. Developers can leverage Spring’s features like @Configuration and @Component while keeping document logic clean and testable.
How to Generate a DOCX in a Spring Boot Service?
@Service
public class InvoiceService {
public byte[] generateInvoice(InvoiceData data) {
var template = OfficeTemplate.fromFile("invoice-template.docx");
var result = template.render(data);
return result.toByteArray();
}
}