# Applying and Updating Formatting Styles from Master Templates (DOTX) in DOCX Documents

> This article shows how to apply and update formatting styles from a master template (DOTX) in a DOCX document using TX Text Control .NET for Windows Forms. Changes in master templates are automatically updated in all associated documents.

- **Author:** Bjoern Meyer
- **Published:** 2024-10-10
- **Modified:** 2025-11-16
- **Description:** This article shows how to apply and update formatting styles from a master template (DOTX) in a DOCX document using TX Text Control .NET for Windows Forms. Changes in master templates are automatically updated in all associated documents.
- **6 min read** (1015 words)
- **Tags:**
  - Windows Forms
  - WPF
  - Formatting Styles
  - Templates
- **Web URL:** https://www.textcontrol.com/blog/2024/10/10/applying-and-updating-formatting-styles-from-master-templates-dotx-in-docx-documents/
- **LLMs URL:** https://www.textcontrol.com/blog/2024/10/10/applying-and-updating-formatting-styles-from-master-templates-dotx-in-docx-documents/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2024/10/10/applying-and-updating-formatting-styles-from-master-templates-dotx-in-docx-documents/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TXTextControl.Windows.Forms.MasterTemplate

---

Using DOTX (Word Document Templates) provides several advantages for document creation and management, especially in environments that require standardized documents or repetitive content. DOTX files are used to store predefined styles for headings, paragraphs, fonts, colors, and layouts. This ensures that every document generated from the template has a consistent look and feel, which is critical for branding or professional consistency.

DOTX files can be loaded into TX Text Control just like any other Office Open XML document. In this example, we will show you how to apply styles from a template to a document, and how to update existing styles in use when they have been changed in the master template.

### Sample Implementation

Consider a template that contains styles for elements such as title, heading, and quotes.

![DOTX Template Styles](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/10/a/assets/template1.webp "DOTX Template Styles")

This document is stored as a DOTX file in a central location that can be accessed by other applications or users.

The sample application consists of two TextControl instances that are placed side by side. On the left side, the user is creating a new document, and on the right side is the master template. This setup is for demonstration purposes only. In a real application, creating and modifying the template is a completely different process. In the beginning, the document on the left is empty and doesn't contain any styles.

![DOTX Template Styles](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/10/a/assets/template2.webp "DOTX Template Styles")

After clicking *Apply Template* on the left side, the styles from the template are applied to the document. All styles are created and available to the user.

![DOTX Template Styles](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/10/a/assets/template3.webp "DOTX Template Styles")

Now the user can choose from the added paragraph and inline styles and format the document accordingly.

![DOTX Template Styles](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/10/a/assets/template4.webp "DOTX Template Styles")

### Updating Styles

If the master template is updated with new styles or existing styles are changed, the user can update the document with the new styles by clicking *Apply Template*. In the next screenshot, the *Title* paragraph style has been updated to center, magenta text color, and increased spacing to the next element. After the modified template has been applied to the document, any style changes will be automatically applied to the document.

![DOTX Template Styles](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/10/a/assets/template5.webp "DOTX Template Styles")

This allows you to maintain a single style in a central location and automatically update all documents based on that template.

### TemplateHandling Class

Let's take a look at the code required to update formatting styles. The following code is executed when the user clicks the *Apply Template* button.

```
byte[] templateDocument;
textControl2.Save(out templateDocument, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
TXTextControl.TemplateHandling.ApplyTemplate(templateDocument, textControl1);
```

*textControl2* is the instance where the master template is maintained and the content is stored in the internal TX Text Control format. Then, the template and the instance of the *TextControl* (*textControl1* in this case) that should be used to apply the formatting styles are passed to the *ApplyTemplate* method.

The *ApplyTemplate* method creates a *ServerTextControl* to load the template from the specified byte array. It loops through all ParagraphStyle and InlineStyle objects and creates the style in the target *TextControl* object if it doesn't exist or updates the existing style.

```
public static void ApplyTemplate(byte[] template, TextControl textControl)
{
    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();
        tx.Load(template, BinaryStreamType.InternalUnicodeFormat);

        // Copy the paragraph and inline styles from the template to the target TextControl
        foreach (ParagraphStyle style in tx.ParagraphStyles)
        {
            var existingStyle = textControl.ParagraphStyles.GetItem(style.Name);

            if (existingStyle == null)
            {
                textControl.ParagraphStyles.Add(new ParagraphStyle(style));
            }
            else if (style.Name != "[Normal]")
            {
                CopyProperties(style, existingStyle);
                existingStyle.Apply();
            }
        }

        foreach (InlineStyle style in tx.InlineStyles)
        {
            var existingStyle = textControl.InlineStyles.GetItem(style.Name);

            if (existingStyle == null)
            {
                textControl.InlineStyles.Add(new InlineStyle(style));
            }
            else
            {
                CopyProperties(style, existingStyle);
                existingStyle.Apply();
            }
        }

    }
}
```

The recursive *CopyProperties* method uses Reflection to loop through all of the properties of the FormattingStyle and apply them to the target style.

```
private static void CopyProperties(object source, object target)
{
    if (source == null || target == null)
        throw new ArgumentNullException("Source or target object is null.");

    Type type = source.GetType();

    // Ensure both objects are of the same type
    if (type != target.GetType())
        throw new ArgumentException("Source and target must be of the same type.");

    // Get all public properties of the type
    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (PropertyInfo property in properties)
    {
        // Check if the property is writable
        if (property.CanWrite)
        {
            object value = property.GetValue(source, null);

            if (value != null && IsComplexType(property.PropertyType))
            {
                // If the property is a class and not a primitive or string, recursively copy
                object targetValue = property.GetValue(target, null) ?? Activator.CreateInstance(property.PropertyType);
                CopyProperties(value, targetValue);
                property.SetValue(target, targetValue);
            }
            else
            {
                try
                {
                    // Copy the value to the target object
                    property.SetValue(target, value);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}
```

At the end, all of the styles in the master template are either created or modified in the target document.

> **Pro Tip**
> 
> You can either save the template names in your application or save the template name and location in the document itself. To do this, meta information can be used, which is stored as document properties in the document itself.
> 
> [Different Ways to Store Additional Information in Documents ](https://www.textcontrol.com/blog/2022/04/15/different-ways-to-store-additional-information-in-documents/llms-full.txt)

### Conclusion

Using DOTX files as templates for document creation provides a powerful way to standardize document formatting and layout. By applying styles from a master template, you can ensure that all documents created from the template have a consistent look and feel. When the master template is updated, all documents based on that template can be automatically updated with the new styles.

TX Text Control provides a comprehensive API to apply styles from a template to a document and to update existing styles when the master template has been changed.

Download the sample from GitHub and test it on your own.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [TXTextControl.Markdown.Core 34.1.0-beta: Work with Full Documents, Selection, and SubTextParts](https://www.textcontrol.com/blog/2026/04/14/txtextcontrol-markdown-core-34-1-0-beta-work-with-full-documents-selection-and-subtextparts/llms.txt)
- [TX Spell .NET 11.0 SP1 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/04/08/tx-spell-net-11-0-sp1-is-now-available/llms.txt)
- [TX Text Control 34.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/02/18/tx-text-control-34-0-sp2-is-now-available/llms.txt)
- [TX Text Control 34.0 SP1 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/12/03/tx-text-control-34-0-sp1-is-now-available/llms.txt)
- [Introducing TX Text Control 34.0: Your Next Leap in Document Processing](https://www.textcontrol.com/blog/2025/11/10/introducing-tx-text-control-34-0-your-next-leap-in-document-processing/llms.txt)
- [Sneak Peek: TX Text Control 34.0 Coming November 2025](https://www.textcontrol.com/blog/2025/10/02/sneak-peek-tx-text-control-34-0-coming-november-2025/llms.txt)
- [TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/08/14/tx-text-control-33-0-sp3-is-now-available/llms.txt)
- [TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/06/18/tx-text-control-33-0-sp2-is-now-available/llms.txt)
- [Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format](https://www.textcontrol.com/blog/2025/05/16/document-lifecycle-optimization-leveraging-tx-text-controls-internal-format/llms.txt)
- [Expert Implementation Services for Legacy System Modernization](https://www.textcontrol.com/blog/2025/05/07/expert-implementation-services-for-legacy-system-modernization/llms.txt)
- [Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5](https://www.textcontrol.com/blog/2025/05/07/service-pack-releases-whats-new-in-tx-text-control-33-0-sp1-and-32-0-sp5/llms.txt)
- [Top 5 Real-World Applications for TX Text Control Document Processing Libraries](https://www.textcontrol.com/blog/2025/04/01/top-5-real-world-applications-for-tx-text-control-document-processing-libraries/llms.txt)
- [DWX Developer Week Moves to Mannheim - And Text Control Is on Board!](https://www.textcontrol.com/blog/2025/03/19/dwx-developer-week-moves-to-mannheim-and-tx-text-control-is-on-board/llms.txt)
- [The Wait is Over: TX Text Control for Linux is Officially Here](https://www.textcontrol.com/blog/2025/03/12/the-wait-is-over-tx-text-control-for-linux-is-officially-here/llms.txt)
- [Full .NET 9 Support in Text Control .NET Components for ASP.NET Core, Windows Forms, and WPF](https://www.textcontrol.com/blog/2024/11/11/full-net-9-support-in-text-control-net-components-for-asp-net-core-windows-forms-and-wpf/llms.txt)
- [Toggle Field Codes in TX Text Control](https://www.textcontrol.com/blog/2024/11/07/toggle-field-codes-in-tx-text-control/llms.txt)
- [TX Text Control 32.0 Service Pack 4 Released](https://www.textcontrol.com/blog/2024/09/02/tx-text-control-32-0-service-pack-4-released/llms.txt)
- [Reliably Detect Property Changes in Objects Using Serialization](https://www.textcontrol.com/blog/2024/07/23/reliably-detect-property-changes-in-objects-using-serialization/llms.txt)
- [Service Pack 3: MailMerge Supports SVG Images](https://www.textcontrol.com/blog/2024/04/29/service-pack-3-mailmerge-supports-svg-images/llms.txt)
- [TX Text Control 32.0 Service Pack 3 Released](https://www.textcontrol.com/blog/2024/04/29/tx-text-control-32-0-service-pack-3-released/llms.txt)
- [Electronic Invoicing will Become Mandatory in Germany in 2025](https://www.textcontrol.com/blog/2024/04/10/electronic-invoicing-will-become-mandatory-in-germany-in-2025/llms.txt)
- [Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#](https://www.textcontrol.com/blog/2024/04/09/inserting-mergeblocks-with-the-datasourcemanager-and-applying-table-styles-in-csharp/llms.txt)
- [The Power of SubTextParts: Typical Use Cases](https://www.textcontrol.com/blog/2024/02/09/the-power-of-subtextparts-typical-use-cases/llms.txt)
- [Renaming Merge Blocks and Merge Fields Programmatically in C#](https://www.textcontrol.com/blog/2024/02/08/renaming-merge-blocks-and-merge-fields-programmatically-in-csharp/llms.txt)
- [TX Text Control Supports .NET 8 in Windows Forms and WPF Applications](https://www.textcontrol.com/blog/2024/01/17/tx-text-control-supports-net-8-in-windows-forms-and-wpf-applications/llms.txt)
