Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active March 8, 2016 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/1b36ab87eca5563da830 to your computer and use it in GitHub Desktop.
Save bjoerntx/1b36ab87eca5563da830 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment