How to Remove Highlighted and Shaded Color of Field using .NET

There are some highlighted text and shading text in my document, I want to remove the highlight and shading color. Also, there is some text with outline levels, I want to remove their outline property so that they look like normal or body text. Finally, what I want is any text in the document should have no highlighting color or shading color, and there is no outline text neither.

I use the following code, but nothing has changed in the output file. Could anyone help? Thanks.

Here is the code in C#:

var doc = new Document(@"D:\SampleDoc.docx");

// a) remove highlight
var highlightedRuns = doc.GetChildNodes(NodeType.Run, true).OfType<Run>().Where(m => m.Font.HighlightColor != Color.Empty);
foreach (Run run in highlightedRuns)    // 7 items in the list (from debug view)
{
    run.Font.HighlightColor = Color.Empty;
}

// b) remove shading
var shadingRuns = doc.GetChildNodes(NodeType.Run, true).OfType<Run>().Where(m => m.Font.Shading.BackgroundPatternColor != Color.Empty);
foreach (Run run in shadingRuns)   // 7 items in the list 
{
    run.Font.Shading.BackgroundPatternColor = Color.Empty;
}

// 3) outline -> body text
var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).OfType<Paragraph>();
var outlines = paragraphs.Where(m => m.ParagraphFormat.OutlineLevel >= OutlineLevel.Level1
                                && m.ParagraphFormat.OutlineLevel <= OutlineLevel.Level9).ToList();
foreach (Paragraph p in outlines)
{
    p.ParagraphFormat.OutlineLevel = OutlineLevel.BodyText;
}

doc.Save(@"D:\New.docx");

The sample file:
SampleDoc.zip (11.4 KB)

@WPInfo

The highlighted color is applied to FieldStart and FieldEnd nodes as well. Please add following code snippet to your application to achieve your requirement.

foreach (Field field in doc.Range.Fields)   
{
    field.Start.Font.HighlightColor = Color.Empty;
    field.End.Font.HighlightColor = Color.Empty;
} 

Please call ListFormat.RemoveNumbers method as shown below to get the desired output.

// 3) outline -> body text
var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).OfType<Paragraph>();
var outlines = paragraphs.Where(m => m.ParagraphFormat.OutlineLevel >= OutlineLevel.Level1
                                && m.ParagraphFormat.OutlineLevel <= OutlineLevel.Level9).ToList();
foreach (Paragraph p in outlines)
{
    p.ParagraphFormat.OutlineLevel = OutlineLevel.BodyText;
    p.ListFormat.RemoveNumbers(); 
}

Thanks for your reply. @tahir.manzoor

I tried the above code, while both of them do not work. I am using Aspose.Words v20.12.0 from NuGet.

@WPInfo

We have not found the shared issue while using the latest version of Aspose.Words for .NET 20.12. Could you please ZIP and attach your problematic and expected output Word documents? We will investigate this issue further and provide you more information on it.

Test.zip (31.3 KB)

Thanks.

The attachment is a console app containing two documents: SampleDoc.docx and result.docx, the first one is what I want to process, the second one is the processed file which still contains highlight, shading color, and outline levels.

@WPInfo

You commented the code in the shared application. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + @"SampleDoc.docx");

// a) remove highlight
var highlightedRuns = doc.GetChildNodes(NodeType.Run, true).OfType<Run>().Where(m => m.Font.HighlightColor != Color.Empty);
foreach (Run run in highlightedRuns)    // 7 items in the list (from debug view)
{
    run.Font.HighlightColor = Color.Empty;
}

foreach (Field field in doc.Range.Fields)
{
    field.Start.Font.HighlightColor = Color.Empty;
    field.End.Font.HighlightColor = Color.Empty;

    field.Start.Font.Shading.ClearFormatting();
    field.End.Font.Shading.ClearFormatting();
}



// b) remove shading
var shadingRuns = doc.GetChildNodes(NodeType.Run, true).OfType<Run>().Where(m => m.Font.Shading.BackgroundPatternColor != Color.Empty);
foreach (Run run in shadingRuns)   // 7 items in the list 
{
    run.Font.Shading.BackgroundPatternColor = Color.Empty;
}

// 3) outline -> body text
var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).OfType<Paragraph>();
var outlines = paragraphs.Where(m => m.ParagraphFormat.OutlineLevel >= OutlineLevel.Level1
                                && m.ParagraphFormat.OutlineLevel <= OutlineLevel.Level9).ToList();
foreach (Paragraph p in outlines)
{
    p.ParagraphFormat.OutlineLevel = OutlineLevel.BodyText;
    p.ListFormat.RemoveNumbers();
}

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.Compliance = OoxmlCompliance.Iso29500_2008_Strict;

doc.UpdateListLabels();
// 'Save As' into DOCX format 
doc.Save(MyDir + @"20.12.docx");