I'm trying to override the style of an imported heading1 section THAT ALREADY HAS THE COLOR CHANGED, but the following code isn't successful. Any idea?
// destination Heading1 style has RED and other formatting
var destination = new WordDocument();
var sourceStyle = Style.CreateBuiltinStyle(BuiltinStyle.Heading1, destination) as Style;
sourceStyle.CharacterFormat.TextColor = Color.Red;
destination.Styles.Add(sourceStyle);
// source Heading1 style has GREEN and other formatting
WordDocument source = new WordDocument(@"source.docx");
destination.ImportContent(source, ImportOptions.UseDestinationStyles);
source.Close();
// loop and change all Heading1 section to be RED
foreach (WSection section in destination.Sections)
{
var body = section.Body;
for (int i = 0; i < body.ChildEntities.Count; i++)
{
IEntity entity = body.ChildEntities[i];
switch (entity.EntityType)
{
case EntityType.Paragraph:
var paragraph = entity as WParagraph;
var destinationStyle = paragraph.GetStyle();
if (destinationStyle.Name == "Heading 1")
{
//paragraph.ApplyStyle(sourceStyle.Name); // This doesn't work neither
paragraph.ApplyStyle(BuiltinStyle.Heading1);
}
break;
}
}
}
destination.Save("destination.docx");
destination.Close();
// PROBLEM - the "destination.docx" Heading1 paragraphs still have GREEN. It's interesting that any source Heading1 paragraph with no special styles applied, e.g. bold, italic, etc., will now be RED.