We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

I don't want to override the correct color

Hi Team,
I am trying to apply the font color for heading 1 I used the below-mentioned code. here I am facing one problem in the heading-"1. TABLE OF CONTENTS " TABLE -red color, OF CONTENTS - Dark Blue color.
The below code is not working for the above case.
Note: I don’t want to override the correct data.

Please find the below input and expected output.
Expected output Table of Contents-.docx (14.6 KB)
Input Table of Contents.docx (14.6 KB)

Style heading1 = ruleBaseModel.SourceDocument.Styles[StyleIdentifier.Heading1];
var value = "Dark Blue";
var isColor = heading1.Font.Color.Name == "ff2f5496" ? "Dark Blue" : "Othercolor";

if (isColor != value.Trim())
    heading1.Font.Color = Color.DarkBlue;

@Princeshivananjappa In your heading paragraph color is set on Run level, i.e. there are 2 runs in the heading paragraph with explicit formatting.

<w:p w14:paraId="17204688" w14:textId="77777777" w:rsidR="003C7830" w:rsidRPr="0002611C" w:rsidRDefault="003C7830" w:rsidP="003C7830">
		<w:pStyle w:val="Heading1"/>
	<w:r w:rsidRPr="0002611C">
		<w:t xml:space="preserve">Table </w:t>
	<w:r w:rsidRPr="003C7830">
			<w:color w:val="002060"/>
		<w:t xml:space="preserve">of Contents </w:t>

Explicit formatting always overrides formatting applied via style. So if you need the runs in the heading paragraph inherit formatting applied in the heading style, you should clear their explicit formatting. For example see the following code:

// Clear explicit formatting applied to the heading paragraphs runs.
doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.ParagraphFormat.IsHeading).ToList()
        .ForEach(p => { p.Runs.Cast<Run>().ToList().ForEach(r => r.Font.ClearFormatting()); });

if I use the ClearFormatting() means it will clear all the existing font properties right?

@Princeshivananjappa Yes, it will clear all explicit formatting of the run and it will inherit formatting from the style applied to the paragraph.