Dynamically Changning Font and Font Size in Aspose word document for whole document

Hi ,

Wanted to change whole document font and font size (Simsun font and size 8) on certain condition like depends on some language need to change font.

Thanks

@SonalCR

To change the font and font size for an entire document in Aspose.Words based on certain conditions, you can iterate through all the paragraphs and runs in the document and apply the desired formatting. Below is a sample code snippet demonstrating how to achieve this using C#:

using Aspose.Words;
using Aspose.Words.Font;

public void ChangeFontAndSize(Document doc, string fontName, double fontSize)
{
    // Loop through all sections in the document
    foreach (Section section in doc.GetChildNodes(NodeType.Section, true))
    {
        // Loop through all paragraphs in the section
        foreach (Paragraph paragraph in section.Body.Paragraphs)
        {
            // Loop through all runs in the paragraph
            foreach (Run run in paragraph.Runs)
            {
                // Change the font name and size
                run.Font.Name = fontName;
                run.Font.Size = fontSize;
            }
        }
    }
}

// Example usage
Document doc = new Document("path/to/your/document.docx");
ChangeFontAndSize(doc, "Simsun", 8);
doc.Save("path/to/your/modified_document.docx");

In this code, the ChangeFontAndSize method takes a Document object, the desired font name, and the font size as parameters. It iterates through each section, paragraph, and run in the document, applying the specified font and size to each run. Finally, the modified document is saved to a specified path.

Make sure to adjust the path to your document accordingly. This approach allows you to dynamically change the font and size based on any conditions you may have in your application logic.

If you have any further questions or need additional assistance, feel free to ask!

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

@SonalCR 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";
}

Thanks @alexey.noskov , is there any option for setting the font size as well like the font?

@SonalCR You can use the same approach, simply change ResetFont to change font size.

private void ResetFont(Font font)
{
    font.Size = mFontSize;
}

Thanks @alexey.noskov for the help it’s working.

1 Like