首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >[C#][原创]How to change a word file font name and size with Aspose.word?

[C#][原创]How to change a word file font name and size with Aspose.word?

作者头像
云未归来
发布2025-07-18 15:18:36
发布2025-07-18 15:18:36
810
举报

I have a word file.so I want to use aspose.word to change all of the text font size and font name.How I can do?I think DocumentBuilder cannot be used in this situation.DocumentBuilder can set font when you generate text,but now,I have a word file,it has contaioned text!

You can build logic on the following code to change the Font name and Font size of entire Word document:

代码语言:javascript
复制
Document doc = new Document("C:\\temp\\input.docx");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save("C:\\temp\\21.1.docx");

代码语言:javascript
复制
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 = mNewFontName;
        font.Size = mNewFontSize;
    }

    private string mNewFontName = "Times New Roman";
    private double mNewFontSize = 8;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档