Remove highlight from document

Hi,

I am currently working on highlight removal from document.

I got a question about that: Is there is any better way to remove highlight from document, NOT by running through nodes that have font field?

I tried to find a method to remove it, however i founded examples with removal highlight only from run nodes(Or better to say inline node -https://reference.aspose.com/words/net/aspose.words/node/ here is a link with nodes hierarchy).

Here is the following code, i take all the runs and apply following code:

Inline inlineNode = (Inline) node;
inlineNode.Font.HighlightColor = _defaultHighlightColor;
inlineNode.Font.Shading.BackgroundPatternColor = _defaultHighlightColor;

However, i got an issue with my document I tried to remove highlight from special chars, field start, field end, runs but it didn’t helped.

Could you please provide me with some insight how to do that correctly(My document is in attachments)?

Thanks.
–Oleh

Hi

Thanks for your inquiry. You can do it using DocumentVisitor. Please try using the following code:

// Open document.
Document doc = new Document("C:\\Temp\\test2.docx");
ChangeHighlightColor changeHighlightColor = new ChangeHighlightColor(Color.Transparent);
doc.Accept(changeHighlightColor);
doc.Save("C:\\Temp\\out.docx");
class ChangeHighlightColor: DocumentVisitor
{
    public ChangeHighlightColor(System.Drawing.Color highlighColor)
    {
        mHighlighColor = highlighColor;
    }
    ///
    /// Called when a FieldEnd node is encountered in the document.
    ///
    public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)
    {
        // Simply change font name
        ChangeFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a FieldSeparator node is encountered in the document.
    ///
    public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)
    {
        ChangeFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a FieldStart node is encountered in the document.
    ///
    public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)
    {
        ChangeFont(fieldStart.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a Footnote end is encountered in the document.
    ///
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ChangeFont(footnote.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a FormField node is encountered in the document.
    ///
    public override VisitorAction VisitFormField(Aspose.Words.Fields.FormField formField)
    {
        ChangeFont(formField.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a Paragraph end is encountered in the document.
    ///
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ChangeFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a Run node is encountered in the document.
    ///
    public override VisitorAction VisitRun(Run run)
    {
        ChangeFont(run.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a SpecialChar is encountered in the document.
    ///
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ChangeFont(specialChar.Font);
        return VisitorAction.Continue;
    }
    private void ChangeFont(Font font)
    {
        font.HighlightColor = mHighlighColor;
    }
    private System.Drawing.Color mHighlighColor = System.Drawing.Color.Transparent;
}

Best regards,