how to change font size of word document using .net

11,269

Solution 1

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;

Solution 2

If you are using 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;
Share:
11,269
pavan betageri
Author by

pavan betageri

Updated on June 05, 2022

Comments

  • pavan betageri
    pavan betageri almost 2 years

    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.

  • pavan betageri
    pavan betageri about 9 years
    Thank you so much...its working...now my doubt is can we add image into header of word document using Microsoft.Office.Interop.Word??....please suggest me some solutions...thank in advance
  • RagtimeWilly
    RagtimeWilly about 9 years
    @pavanbetageri I've edited answer to include information about adding an image. Can you be sure to accept as answer if this solved your problem? :)
  • pavan betageri
    pavan betageri about 9 years
    @RagtimeWilly...sure..ill try this code today...thank you so much
  • pavan betageri
    pavan betageri about 9 years
    @RagtimeWilly...its working, can i do the same using spire.doc for entire word document??
  • pavan betageri
    pavan betageri about 9 years
    how can i do this for entire word document using spire.doc