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 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();
}
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.
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.