您好 关于替换字体的问题


#1

您好 我想问一下怎么将一篇文档中所有内容字体替换为某一种字体,其他格式不变,就只替换字体, 比如“仿宋”,麻烦指导一下 谢谢


#2

@sun1299shine

感谢您与支持人员联系。

您能否解释一下您使用的文件格式? 它是DOC / DOCX / PDF还是其他格式? 您是在.NET / Java或其他环境中工作吗? 还要共享包含输入文件和字体文件的ZIP文件,以便我们进一步调查。


#3

好的 文件格式有可能是doc 有可能是docx 开发环境是在.net 语言为c# 谢谢


#4

@sun1299shine

请使用以下代码示例更改整个文档的字体。

Document doc = new Document(MyDir + "in.docx");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save(MyDir + @"output.docx");

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;
    }

    private string mNewFontName = "Arial";
}

#5

可以了 真的非常感谢哦!