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

Free Support Forum - aspose.com

How to change font color of Style using C#

Hi, I am using Aspose to load a file and download it in my local machine, by using:

Document doc = new Document(filePath);
doc.Save(anotherFilePath);

So i am wonder if I can change the default font color for the downloaded file? Like when I open the file and start typing, the font color will be blue instead of black?

Thanks.

@victorxxx

You can use following code example to change the font color and name of whole document. Hope this helps you.

If you still face problem, please share some more detail about your requirement along with input and expected output documents. We will then answer your query accordingly.

Document doc = new Document(MyDir + "in.docx");
FontChanger changer = new FontChanger();
doc.Accept(changer);

doc.Save(MyDir + "out.docx");

class FontChanger : DocumentVisitor
{
    ///
    /// Called when a FieldEnd node is encountered in the document.
    ///
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        ResetFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FieldSeparator node is encountered in the document.
    ///
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        ResetFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FieldStart node is encountered in the document.
    ///
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        ResetFont(fieldStart.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Footnote end is encountered in the document.
    ///
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ResetFont(footnote.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FormField node is encountered in the document.
    ///
    public override VisitorAction VisitFormField(FormField formField)
    {
        ResetFont(formField.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Paragraph end is encountered in the document.
    ///
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ResetFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Run node is encountered in the document.
    ///
    public override VisitorAction VisitRun(Run run)
    {
        ResetFont(run.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a SpecialChar is encountered in the document.
    ///
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ResetFont(specialChar.Font);
        return VisitorAction.Continue;
    }

    private void ResetFont(Aspose.Words.Font font)
    {
        font.Name = mNewFontName;
        font.Color = mNewColor;
    }
    private Color mNewColor = Color.Black;
    private string mNewFontName = "Times New Roman";
}

Thanks for your response. However, I don’t want to change the font color of original document. What I want to do is to change the default color to blue(maybe), and the new text I type will be blue while the original document content is still black.

@victorxxx

Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style .
  2. to the parent of those Run nodes i.e. a Paragraph node ( possibly via paragraph Styles ).
  3. you can also apply direct formatting to Run nodes by using Run attributes ( Font ). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

In your case, we suggest you please change the default font color of Normal style to achieve your requirement. Please check the following code example. Hope this helps you.

var doc = new Aspose.Words.Document();
doc.Styles[StyleIdentifier.Normal].Font.Color = Color.Blue;

doc.Save(MyDir + "output.docx");