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:
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.
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)