How to set one default font: Arial throughout the page

I am using MS Word Template to create a pdf using Aspose. In MS Word, there are three different fonts that I can’t change. Is there a way in Aspose that I could change all the fonts in the document to Arial so that when it is converted pdf, it will all be in Arial.

I tried following code but it didn’t work.

Dim font As Aspose.Words.Font = dbMainDocBuilder.Font
font.Name = "Arial"

Hi,

Thanks for your inquiry.

If you would like to change font of the entire document, you should loop through all nodes and change font. You can use DocumentVisitor to achieve this. Here is code example:

Document doc = new Document(@"C:\Temp\input.doc");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save(@"C:\Temp\out.doc");
///
/// Class inherited from DocumentVisitor, that chnges font.
///
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 = mNewFont;
    }
    // Font by default
    private string mNewFont = "Arial";
}

I hope, this helps.

Best regards,