We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to override style of an imported heading1 section?

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.

1 Reply

MJ Mohanaselvam Jothi Syncfusion Team June 24, 2019 09:06 AM UTC

Hi Trevor,

Thank you for contacting Syncfusion support.

We have tried to reproduce the reported problem using the given details and code example, but it works properly at our end. For your reference, we have attached the sample application which we tried at our end and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/145466/ze/Sample_application565484951

We suspect that the problem might be due to the formats applied in input Word document (in file level). So, could you please share us the following details from your end:
  1. Input Word document.
  2. Output document generated at your end
  3. Expected Word document.
Based on the above details, we will proceed further to check on the problem and will provide you appropriate solution at the earliest.

Note: If you have any confidential data in your Word document, please replace with some dummy data and provide us the same. We just need your document to recreate the problem you face.

Regards,
Mohanaselvam J 


Loader.
Live Chat Icon For mobile
Up arrow icon