About changing font name and size in Aspose.word template

Hi Team,

Could you pls help me on how to change font name and size in Aspose.word template having font setting.

@rose.thomas

Summary
You can change the font name and size in an Aspose.Words template by modifying the document’s styles, by iterating over the Run nodes, or by setting a default font for any new content you add. The change should be applied before you perform further processing such as mail‑merge; existing text will only be affected if you modify the styles or runs that control it.

Why it happens

  • FontSettings.DefaultFontName (or Document.DefaultFontName) only influences newly created runs. Text that already exists in the template keeps its original formatting.
  • Most templates rely on styles (e.g., Normal, Heading 1) to define font attributes. Changing a global default does not override those style definitions.
  • The FontSettings class is mainly for font substitution (choosing a fallback when a requested font is missing); it does not change the size of existing text.
  • Therefore, to see the font change throughout the document you need to either:
  1. Update the relevant styles in the template, or
  2. Iterate over all Run nodes and set Run.Font.Name and Run.Font.Size directly.

What to do next

  1. Load the template
var doc = new Document("MailMergeTemplate.docx");
  1. Option A – Set a default font for any new content you add
var fontSettings = new FontSettings();
fontSettings.DefaultFontName = "Arial"; // desired font name
doc.FontSettings = fontSettings; // attach to the document

Note: This affects only text that you create after this point (e.g., new runs inserted during mail‑merge).

  1. Option B – Update a specific style (recommended for most templates)
// Example: modify the built‑in "Normal" style
Style normal = doc.Styles[StyleIdentifier.Normal];
normal.Font.Name = "Arial";
normal.Font.Size = 12; // points

For custom styles, replace "Normal" with the style name:

Style myStyle = doc.Styles["MyCustomStyle"];
myStyle.Font.Name = "Calibri";
myStyle.Font.Size = 11;
  1. Option C – Change every existing run (useful when the template mixes formatting)
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
 run.Font.Name = "Arial";
 run.Font.Size = 12; // points
}
  1. Perform the mail‑merge (or other processing) after the font changes
doc.MailMerge.Execute(
 new[] { "Name", "City" },
 new object[] { "Hasan", "Almaty" });

doc.Save("Result.docx"); // or .pdf, .html, etc.
  1. Verify the result – Open the saved document in Microsoft Word (or the target format) to confirm that the font name and size have been applied as expected.

If you have already tried one of these approaches and the font still does not change, please share the exact code snippet you are using. That will help identify whether the issue is due to style precedence, the timing of the change, or another factor.

Note: The retrieved evidence does not contain explicit documentation about changing fonts in Aspose.Words templates; the guidance above is based on typical usage patterns of the Aspose.Words API.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

Thanks for the solution but wanted something which will change the existing font and size without checking any sections like Paragraphs or other section.

@rose.thomas If your goal is to change font in whole document, you can use DocumentVisitor to change font. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
doc .Accept(new FontChanger("Arial"));
doc.Save("C:\\Temp\\out.docx");
internal class FontChanger : DocumentVisitor
{
    public FontChanger(string fontName)
    {
        mFontName = fontName;
    }

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

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

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

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

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

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

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

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

    private void ResetFont(Font font)
    {
        font.Name = mFontName;
    }

    private string mFontName = "Arial";
}

You can change the above code to change other font properties or change font in particular document part.

1 Like

Thank you , is there any option for setting the font size separately for headings and details

@rose.thomas Unfortunately, your requirement is not clear enough. If possible, could you please attach your sample input and expected output?

for example, in the below mentioned image, headings like “applied voltage”,test condition,frequency (Hz), etc… labels needs in font size 12 and binded data we need in font size 8. can we able to set dynamically? I have attached created template screenshot as well.

template:

@rose.thomas

Although it is possible to apply dynamic text formatting by using an html switch like that: <<["<b>Bold Text</b>"] -html>>, I see no need to go that route in this case. For example, you can select text <<[lang.GetString("AppliedVoltage")]>> in your template and assign it any font size that you need in a result report - and LINQ Reporting Engine will use that font size.