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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to override style of an imported heading1 section?

Thread ID:

Created:

Updated:

Platform:

Replies:

145466 Jun 21,2019 06:52 PM UTC Jun 24,2019 09:06 AM UTC WinForms 1
loading
Tags: DocIO
Trevor Nguyen
Asked On June 21, 2019 06:52 PM UTC

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.

Mohanaselvam  Jothi [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon