如何使用.net更改word文档的字体大小

时间:2015-03-21 20:17:49

标签: c# fonts spire.doc

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

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

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

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

2 个答案:

答案 0 :(得分:2)

您需要使用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;

答案 1 :(得分:1)

如果您使用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;