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.

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.
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.
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.
Now the user can choose from the added paragraph and inline styles and format the document accordingly.
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.
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 Paragraph
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 Formatting
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.
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.
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- TX Text Control .NET for Windows Forms 32.0 SP2 or better
- Visual Studio 2022
Windows Forms
Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.
Related Posts
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format
Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…
Expert Implementation Services for Legacy System Modernization
We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.
Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5
TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…