Hi Team,
Could you pls help me on how to change font name and size in Aspose.word template having font setting.
Hi Team,
Could you pls help me on how to change font name and size in Aspose.word template having font setting.
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.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.Run.Font.Name and Run.Font.Size directly.What to do next
var doc = new Document("MailMergeTemplate.docx");
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).
// 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;
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
run.Font.Name = "Arial";
run.Font.Size = 12; // points
}
doc.MailMerge.Execute(
new[] { "Name", "City" },
new object[] { "Hasan", "Almaty" });
doc.Save("Result.docx"); // or .pdf, .html, etc.
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.
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.
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:
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.