@alexey.noskov How to change enter document Font Name to “SegoeUI”.
@Manasahr Do you mean changing document default font? If so, you can use StyleCollection.DefaultFont property.
@alexey.noskov I tried below code its not working. Kindly help me asap.
Please note i need to change enter document content to segoe UI font name.
// Set the default font family for the document
doc.Styles.DefaultFont.Name = "Segoe UI";
// Loop through all paragraphs in the document and set their font family to the new font family
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
para.ParagraphFormat.Style.Font.Name = "Segoe UI";
}
Please find the below input and expected output document.
Epected_output_1236.docx (16.8 KB)
input_document_12.docx (22.2 KB)
@Manasahr You can use DocumentVisitor to change font in the entire document. For example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.Accept(new FontChanger("Segoe UI"));
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(Aspose.Words.Font font)
{
font.Name = mFontName;
}
private string mFontName = "Arial";
}