---
title: "Create Word documents using C# templates | Nutrient DCS"
canonical_url: "https://www.nutrient.io/guides/document-converter/document-converter-services/create-word-documents/create-word-documents-using-csharp/"
md_url: "https://www.nutrient.io/guides/document-converter/document-converter-services/create-word-documents/create-word-documents-using-csharp.md"
last_updated: "2026-06-08T09:14:14.317Z"
description: "Fill Word templates with JSON data using C# and Nutrient Document Converter Services. Complete code example included."
---

# Create Word documents using C# templates

This guide explains how to use the Nutrient Document Converter Services (DCS) API to fill a Word `.docx` template with data provided through a JSON model. This is especially useful for generating contracts, invoices, reports, or any document requiring templated content.

## Prerequisites

- A `.docx` template file with placeholders such as `{{name}}` and `{{invoicenumber}}`.

- A JSON model defining placeholder-value mappings.

- DCS installed and running.

- Implemented `OpenService()` and `CloseService()` methods from [`DocumentConverterServiceClient`](https://www.nutrient.io/guides/document-converter/document-converter-services/knowledge-base/open-and-close-documentconverterserviceclient/) sample code.

## Template placeholders

A template model is a JSON format structure. It can be either an external JSON file or created programmatically. It includes a configuration section (containing the delimiter characters) and a model section containing at least one placeholder-value pair.

**Sample model**

```json

{
	"config": {
		"delimiter": {
			"start": "{{",
			"end": "}}"
		}
	},
	"model": {
		"name": "Jules Winnfield",
		"invoicenumber": "90121",
		"paymentdue": "28/09/2020",
		"grandtotal": "81.00",
		"yourname": "Johan Schmidt",
		"yourposition": "Finance department",
		"yourcontactdetails": "Systems testing"
	}
}

```

## Sample Word template

## Sample code

```csharp

        /// <summary>
        /// Create new Word documents by filling in Word template files.
        /// </summary>
        /// <param name="ServiceURL">URL endpoint for the Document Converter Services.</param>
        /// <param name="templateFile">Word document template file.</param>
        /// <param name="dataFile">JSON format model file.</param>
        /// <param name="targetFolder">Target folder to receive the output file.</param>
        static void TestTemplate(string ServiceURL, string templateFile, string dataFile, string targetFolder)
        {
            // Read the template file as a byte array.
            byte[] sourceBytes = File.ReadAllBytes(templateFile);
            // Read the JSON data.
            string dataValues = File.ReadAllText(dataFile);

            DocumentConverterServiceClient client = null;

            // Simple `OpenOptions` object.
            OpenOptions openOptions = new OpenOptions();
            openOptions.FileExtension = Path.GetExtension(dataFile);
            openOptions.OriginalFileName = Path.GetFileName(dataFile);
            try
            {
                // Open a client.
                client = OpenService(ServiceURL);

                // Call the `FillWordTemplate` method on DCS.
                BatchResult result = client.FillWordTemplate(sourceBytes, openOptions, dataValues);

                // Check that a result is returned.
                if (result!= null)
                {
                    // Create the output folder if it doesn’t exist.
                    if (!Directory.Exists(targetFolder))
                    {
                        Directory.CreateDirectory(targetFolder);
                    }
                    // Get the filename from the result.
                    string filename = result.FileName;
                    Console.WriteLine($"File {Path.GetFullPath(Path.Combine(targetFolder, filename))} created");
                    // Write the result to the target filename.
                    File.WriteAllBytes(Path.Combine(targetFolder, filename), result.File);
                }
                else
                {
                    Console.WriteLine($"No result returned");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message}");
            }
            finally
            {
                if (client!= null)
                {
                    // Close the service.
                    CloseService(client);
                }
            }
        }

```

The code creates a new Word document by filling the template with the JSON data. The output file is saved to the specified target folder with the filename returned by DCS, maintaining the original `.docx` format.

## Troubleshooting

**Connection error: Cannot connect to DCS service**

- Ensure DCS is running and accessible

- Verify the service URL in your code matches your DCS installation

- Check that no firewall is blocking the connection

**Template processing error: Invalid placeholders**

- Verify that placeholders in your Word template match the JSON model keys exactly

- Ensure placeholder delimiters in the JSON config match those used in the template

- Check that the Word template file is accessible and not corrupted

**JSON data error: Invalid JSON format**

- Validate your JSON file syntax using an online JSON validator

- Ensure all string values are correctly quoted

- Check for trailing commas or missing brackets

**File access error: Permission denied**

- Verify that the application has read access to the template and JSON files

- Check that the output directory has write permissions

- Ensure files aren’t locked by other applications

## What’s next

Now that you can create Word documents from templates, explore these related DCS capabilities with C#:

- **Form filling** - Learn how to [fill PDF forms using C#](https://www.nutrient.io/guides/document-converter/document-converter-services/extraction/extract-data-from-forms/csharp.md) to complete interactive documents

- **Document conversion** - Discover [document conversion with C#](https://www.nutrient.io/guides/document-converter/document-converter-services/conversion/csharp.md) to transform between different formats

- **Document security** - Implement [document security with C#](https://www.nutrient.io/guides/document-converter/document-converter-services/document-security/csharp.md) to protect sensitive information

- **Merging documents** - Combine multiple documents using [merge documents with C#](https://www.nutrient.io/guides/document-converter/document-converter-services/merge-or-combine/csharp.md)
---

## Related pages

- [WSDL URL.](/guides/document-converter/document-converter-services/create-word-documents/create-word-documents-using-python.md)

