How to change font name and font size after export to word in c#

Hi,

I want to set Default Font name and size after exporting data to word.

My Code:
.
Document doc = new Document(fileName);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark(“CorporateGovernance”, false, true);
builder.InsertHtml(txtCP.Text);

Aspose.Words.Fonts.FontSettings fontSettings = new Aspose.Words.Fonts.FontSettings();
fontSettings.DefaultFontName = “Arial”; ;
doc.FontSettings = fontSettings;
string filePath = Dir + ExportFileName;
doc.Save(filePath);

But i am not able to change the font name.

Please check my html file.

Default Font.zip (510 Bytes)

@pravinghadge

Please use the following code example to change the font name of text in whole document and save it to DOCX.

Document doc = new Document(MyDir + @"Default Font.html");

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;
    }

    private string mNewFontName = "Arial";
}

Thank you so much Tahir.manzoor.

Its working.

@pravinghadge

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.