Last active
March 8, 2016 15:33
-
-
Save bjoerntx/1b36ab87eca5563da830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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