If a word document has many pages, people may add header or footer to show the page number. Sometimes, the header or footer may be symbols which are the topic of the document talk about or the owner of the document.

It is simple to insert add header and footer. Open a Word document. Then click Header and footer on File menu. Type the text or use the format Word provides.

Then, I want to show something about how to add Word header and footer with C#.  

Using the code:

#region add Word header and footer
/**//// <summary>
/// Add Header
/// </summary>
/// <param name="filePath">file name</param>
/// <returns></returns>
public static bool AddPageHeaderFooter(string filePath)
{

    Try

    {

        Object oMissing = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word._Application WordApp = new Application();

        WordApp.Visible = true;

        object filename = filePath;

        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

       /**/////Method 1 to Add Header

        //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;

        //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;

        //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**" );//Header Content

        /**/////Method 2 to Add Header
        if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
            WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)        

 {

            WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;

        }

        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;

        WordApp.Selection.HeaderFooter.LinkToPrevious = false;

        WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

        WordApp.Selection.HeaderFooter.Range.Text = "Header Content";

        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;

        WordApp.Selection.HeaderFooter.LinkToPrevious = false;

        WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

        WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("Footer Content");

        //Quit Header and Footer Setting

        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

        //Save file

        WordDoc.Save();

        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);

        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);

        return true;

    }

    catch (Exception e)

   {

        Console.WriteLine(e.Message);

        Console.WriteLine(e.StackTrace);

        return false;

    }
}
#endregion Add Word header and footer

Sometimes, programmers may use components. The components can save programmers much of time to write code and deal with a large amount of documents quickly.

To sum up, in this article, I shows the method to add Word header and footer by using C# and hope it will be helpful for you.