Search code examples
c#fontsspire.doc

how to change font size of word document using .net


I am working on a application using C# and Spire.Doc which saves the word document to specified format which includes logo at the header and specified font size and style.

Now I can paste logo at the header using spire.doc but I'm not able to change the font style and size of the whole document:

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

Can someone please help me for this? Thanks in advance.


Solution

  • You will need to use Microsoft.Office.Interop.Word.

    This will allow you to do something like this:

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

    For more detail see: How to: Programmatically Format Text in Documents.

    EDIT:

    To add an image to the header you need to do something like:

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

    Or:

    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;