【问题标题】:how to change font size of word document using .net如何使用.net更改word文档的字体大小
【发布时间】:2015-03-21 20:17:48
【问题描述】:

我正在使用 C# 和 Spire.Doc 开发一个应用程序,它将 word 文档保存为指定格式,其中包括标题处的徽标以及指定的字体大小和样式。

现在我可以使用 spire.doc 在标题处粘贴徽标,但我无法更改整个文档的 font 样式和 size

font size should be 10;
font should be: franklin gothic demi

有人可以帮我吗? 提前致谢。

【问题讨论】:

    标签: c# fonts spire.doc


    【解决方案1】:

    您需要使用Microsoft.Office.Interop.Word

    这将允许您执行以下操作:

    var start = this.Content.Start;
    var end = this.Content.End;
    
    var docRange = this.Range(ref start, ref end).Select();
    
    docRange.Font.Size = 10; 
    docRange.Font.Name = "Franklin Gothic Demi"; 
    

    更多详情请见:How to: Programmatically Format Text in Documents

    编辑:

    要将图像添加到标题中,您需要执行以下操作:

    section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
           .Shapes
           .AddPicture(@"headerImage.jpg", left, top, width, height);
    

    或者:

    Document doc = new Document();
    doc.LoadFromFile(@"C:\MyDoc.docx", FileFormat.Docx);
    HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
    Image headerImage = Image.FromFile(@"C:\headerImage.png");
    header.Paragraphs[0].AppendPicture(logo).TextWrappingStyle = TextWrappingStyle.Tight;
    

    【讨论】:

    • 非常感谢...它的工作...现在我的疑问是我们可以使用 Microsoft.Office.Interop.Word 将图像添加到 word 文档的标题中吗??...请建议我一些解决方案...提前致谢
    • @pavanbetageri 我已经编辑了答案以包含有关添加图像的信息。如果这解决了你的问题,你能确定接受作为答案吗? :)
    • @RagtimeWilly...当然..我今天试试这个代码...非常感谢
    • @RagtimeWilly...它的工作原理,我可以使用 spire.doc 对整个 word 文档做同样的事情吗??
    【解决方案2】:

    如果您使用的是Spire.Doc

            //Font name
            txtRange.CharacterFormat.FontName = "Century Gothic";
    
            //Size
            txtRange.CharacterFormat.FontSize = 15;
    
            //Underline
            txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
    
            //Color
            txtRange.CharacterFormat.TextColor = Color.Brown;
            txtRange1.CharacterFormat.TextColor = Color.ForestGreen;
    

    【讨论】:

    • 如何使用 spire.doc 对整个 word 文档执行此操作