Remove highlight from document(Aspose Word 删除高亮)

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;
    }
 
    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)
    {
        //Simply change font name
        ChangeFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)
    {
        ChangeFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)
    {
        ChangeFont(fieldStart.Font);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a Footnote end is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ChangeFont(footnote.Font);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a FormField node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFormField(Aspose.Words.Fields.FormField formField)
    {
        ChangeFont(formField.Font);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a Paragraph end is encountered in the document.
    /// </summary>
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ChangeFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a Run node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitRun(Run run)
    {
        ChangeFont(run.Font);
        return VisitorAction.Continue;
    }
 
    /// <summary>
    /// Called when a SpecialChar is encountered in the document.
    /// </summary>
    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,

 

你可能感兴趣的:(Remove highlight from document(Aspose Word 删除高亮))