# Applying Paragraph Styles from a Master Template

> A Windows Forms extension method applies paragraph styles from a master template to a target document by matching style names. A temporary ServerTextControl loads the master template, and the code replaces styles across all TextParts, including main text, headers, and text frames.

- **Author:** Bjoern Meyer
- **Published:** 2016-03-09
- **Modified:** 2026-03-05
- **Description:** A Windows Forms extension method applies paragraph styles from a master template to a target document by matching style names. A temporary ServerTextControl loads the master template, and the code replaces styles across all TextParts, including main text, headers, and text frames.
- **3 min read** (466 words)
- **Tags:**
  - GitHub
  - Windows Forms
- **LLMs.txt URL:** https://www.textcontrol.com/blog/2016/03/09/applying-paragraph-styles-from-a-master-template/llms.txt
- **LLMs-full.txt URL:** https://www.textcontrol.com/blog/2016/03/09/applying-paragraph-styles-from-a-master-template/llms-full.txt
- **Canonical URL:** https://www.textcontrol.com/blog/2016/03/09/applying-paragraph-styles-from-a-master-template/
- **GitHub Repository:** https://github.com/TextControl/TextControl.WindowsForms.MasterTemplate

---

Styles are typically used to change the formatting consistently across a complete document. Specifically when merging templates using the [MailMerge](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.class.htm) 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](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.textpartcollection.class.htm) styled with the custom paragraph styles *Heading 1* (in the main text and text frame) and *Heading 2* (in the header).

![Applying paragraph styles from a master template](https://s1-www.textcontrol.com/assets/dist/blog/2016/03/09/a/assets/tx_style_document.webp "Applying paragraph styles from a master template")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*:

![Applying paragraph styles from a master template](https://s1-www.textcontrol.com/assets/dist/blog/2016/03/09/a/assets/tx_master.webp "Applying paragraph styles from a master template")The following extension method stores all paragraphs with the applied style names. Using a temporary [ServerTextControl](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.servertextcontrol.class.htm) 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:

![Applying paragraph styles from a master template](https://s1-www.textcontrol.com/assets/dist/blog/2016/03/09/a/assets/tx_results.webp "Applying paragraph styles from a master template")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

- [Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub](https://www.textcontrol.com/blog/2023/01/08/official-tx-text-control-net-sample-applications-are-now-hosted-on-github/llms.txt)
- [Inserting Multipage TIFF Images into TX Text Control](https://www.textcontrol.com/blog/2015/09/25/inserting-multipage-tiff-images-into-tx-text-control/llms.txt)
- [Windows Forms: Printing Multiple Pages Per Sheet](https://www.textcontrol.com/blog/2015/07/24/windows-forms-printing-multiple-pages-per-sheet/llms.txt)
- [Inserting Watermark Images to All Pages Dynamically](https://www.textcontrol.com/blog/2015/07/17/inserting-watermark-images-to-all-pages-dynamically/llms.txt)
- [Reporting: Merging MS Word Documents with DocVariables](https://www.textcontrol.com/blog/2015/06/10/reporting-merging-ms-word-documents-with-docvariables/llms.txt)
- [MailMerge: Conditional INCLUDETEXT Fields](https://www.textcontrol.com/blog/2015/01/08/mailmerge-conditional-includetext-fields/llms.txt)
- [Inserting MS Word Compatible FORMCHECKBOX Fields](https://www.textcontrol.com/blog/2010/01/27/inserting-ms-word-compatible-formcheckbox-fields/llms.txt)
- [Change Table Width to Fit Document Width - Automatically](https://www.textcontrol.com/blog/2005/02/03/change-table-width-to-fit-document-width-automatically/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)
- [Building a TX Text Control Project with GitHub Actions and the Text Control NuGet Feed](https://www.textcontrol.com/blog/2026/02/09/building-a-tx-text-control-project-with-github-actions-and-the-text-control-nuget-feed/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)
- [Splitting Tables at Bookmark Positions and Cloning Table Headers](https://www.textcontrol.com/blog/2025/02/13/splitting-tables-at-bookmark-positions-and-cloning-table-headers/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)
- [Loading and Processing Excel XLSX Spreadsheet Tables into TX Text Control using .NET C#](https://www.textcontrol.com/blog/2024/10/16/loading-and-processing-excel-spreadsheet-tables-into-tx-text-control-using-net-csharp/llms.txt)
