Applying Paragraph Styles from a Master Template
Styles are typically used to change the formatting consistently across a complete document. Specifically when merging templates using the MailMerge class, it makes sense to keep all styles in a separate master template in order to apply those styles to all other templates. This sample project shows how to apply the paragraph styles of a master template to a loaded document based on style names. The following screenshot shows a document with 3 paragraphs in different TextParts styled with the…

Styles are typically used to change the formatting consistently across a complete document. Specifically when merging templates using the MailMerge class, it makes sense to keep all styles in a separate master template in order to apply those styles to all other templates.
This sample project shows how to apply the paragraph styles of a master template to a loaded document based on style names. The following screenshot shows a document with 3 paragraphs in different TextParts styled with the custom paragraph styles Heading 1 (in the main text and text frame) and Heading 2 (in the header).

When clicking the menu item Apply Master Template, the extension method ApplyMasterTemplate is called by passing a master template document as a parameter:
textControl1.ApplyMasterTemplate("master.tx", StreamType.InternalUnicodeFormat);
This master template contains two styles with the same names Heading1 and Heading2:

The following extension method stores all paragraphs with the applied style names. Using a temporary ServerTextControl instance, the master template is loaded. All styles with the same name are replaced with the style from the master template. Finally, they are applied to all stored paragraphs.
public static bool ApplyMasterTemplate(this TextControl textControl,
string masterTemplate, StreamType streamType)
{
// list to keep all style names
List<ParStyle> styles = new List<ParStyle>();
using (ServerTextControl serverTextControl =
new ServerTextControl())
{
serverTextControl.Create();
// load the master template
try {
serverTextControl.Load(masterTemplate, streamType);
}
catch {
return false;
}
// loop through all paragraphs to store the used
// style names
foreach (IFormattedText textPart in textControl.TextParts)
{
foreach (Paragraph par in textPart.Paragraphs)
{
ParStyle style = new ParStyle(par, par.FormattingStyle);
styles.Add(style);
}
}
// loop through all paragraph styles and
// replace the style, if the name already exist
foreach (ParagraphStyle style in serverTextControl.ParagraphStyles)
{
if (textControl.ParagraphStyles.GetItem(style.Name) != null)
{
textControl.ParagraphStyles.Remove(style.Name);
// create a new style and add it to TextControl
ParagraphStyle newStyle = new ParagraphStyle(style);
textControl.ParagraphStyles.Add(newStyle);
}
}
}
// apply the stored style names to all paragraphs
foreach (ParStyle par in styles)
{
par.Paragraph.FormattingStyle = par.StyleName;
}
return true;
}
After the styles have been replaced, the applied styles are visible in the original document:

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
- Visual Studio 2015 or better
- TX Text Control .NET for Windows Forms (trial sufficient)
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
Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub
This article gives a quick overview of the new repositories, their structure and our plans for the future.
Inserting Multipage TIFF Images into TX Text Control
The Tagged Image File Format (TIFF) format is used by many scanning applications, publishing and layout applications and scanners and fax machines. Many scanners create multipage documents…
Windows Forms: Printing Multiple Pages Per Sheet
This sample project implements the class MultipagePrintDocument that inherits from System.Drawing.Printing.PrintDocument to print multiple pages of a document per sheet. The constructor of…
Inserting Watermark Images to All Pages Dynamically
This sample project shows how to create and insert a watermark image on all pages dynamically. Image objects have the Name property to store additional string information with an image. This…
Reporting: Merging MS Word Documents with DocVariables
TX Text Control supports all MS Word fields including the typical merge fields, content control fields, legacy form fields or special fields such as DOCVARIABLE. All of those fields can be found…