---
title: "Word template generation and processing | Nutrient Java SDK"
canonical_url: "https://www.nutrient.io/guides/java/templates/word-template-generation/"
md_url: "https://www.nutrient.io/guides/java/templates/word-template-generation.md"
last_updated: "2026-06-09T10:32:42.840Z"
description: "Generate documents from Word templates with JSON data."
---

# Word template generation and processing

Use template generation to create many document variants from one Word template.

It helps you:

- **Separate content from design** — Designers can perfect the document layout while developers focus on data integration.

- **Ensure consistency** — Every generated document follows the exact same format and styling.

- **Scale document output** — Generate many personalized documents from the same template.

- **Reduce errors** — Eliminate copy-paste mistakes and ensure data accuracy through automation.

- **Enable dynamic content** — Automatically adjust document structure based on data (for example, show/hide sections, repeat rows).

[Download sample](https://www.nutrient.io/downloads/samples/java/word-template-generation.zip)

## Use the Java SDK for template processing

The SDK handles low-level document details such as:

- Parsing Word document internals

- Managing XML structures

- Handling style preservation

- Complex placeholder replacement logic

The SDK applies model data to placeholders while preserving template formatting.

## Complete implementation

This example shows a complete template processing flow:

```java

package io.nutrient.Sample;
import io.nutrient.sdk.Document;
import io.nutrient.sdk.License;
import io.nutrient.sdk.exceptions.NutrientException;
import io.nutrient.sdk.editors.WordEditor;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class WordTemplate {

    public static void main(String[] args) throws NutrientException {

```

These lines set up the Java application:

- `package io.nutrient.Sample;` — Declares the package for your class

- The import statements bring in all necessary classes from Nutrient SDK and Java standard library

- `public class WordTemplate` — Defines your main class

- `public static void main(String[] args) throws NutrientException` — The entry point that declares thrown Nutrient-specific exceptions

```java

        try (Document document = Document.open("input_template.docx")) {

```

Open the Word template file. The [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) syntax closes the document automatically:

```java

            WordEditor editor = WordEditor.edit(document);

```

Create a `WordEditor` instance for template operations:

```java

            String model = null;
            try {
                model = Files.readString(Path.of("input_template_model.json"));
            } catch (IOException e) {
                System.out.println("Failed to read template model file");
            }

```

Read the JSON data model:

- `String model = null;` — Initialize the model variable.

- `model = Files.readString(Path.of("input_template_model.json"));` — Read the entire JSON file content into a string.

- The try-catch block handles file read errors.

```java

            editor.applyTemplateModel(model);

```

Apply the JSON model to the template. This replaces placeholders with data while keeping template formatting:

```java

            editor.saveWithModelAs("output.docx");

```

Save the processed document to a new file:

```java

        }
    }
}

```

These closing braces end the try-with-resources block, `main` method, and class definition.

## Conclusion

You now have a complete template processing flow. The core logic has four steps:

- Open the document.

- Create an editor.

- Apply the template model.

- Save the result.

Download the [sample package](https://www.nutrient.io/downloads/samples/java/word-template-generation.zip) to run this example as-is.
---

## Related pages

- [Nutrient Java SDK template guides](/guides/java/templates.md)
- [Word template processing-to-PDF/UA conversion](/guides/java/templates/word-template-to-pdf-ua.md)

