Insert Header in Word Document – C#/VB.NET

Word header, which is often inserted on the top of one page, can display additional information of one document.  Generally speaking, header can be date, time, page number or document title. If the document is company materials, header may be company name.

In this post, I want to introduce a method about how to insert header in Word with C# /VB.NET.

In my example, I prepare a document which doesn’t have a header. I will add document title as header and use a border to separate header and contents.

*The .NET Word component, Spire.Doc for.NET is used to realize this function, so I add its dll file as reference in project at the beginning.

Detailed Step Shown as Following:

  1. Load document which I want to insert header from computer add and a section in document.
  2. Insert header in document. Firstly, declare header and add a header paragraph. Secondly, append text for header. The text is document title. Thirdly, set header format, including font type, size, color and alignment. Finally, add border under header. Set border type and distance between border and header text.
  3. Save and launch file.

Main Coding:


using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

using Spire.Doc.Fields;
namespace InsertHeader
    class header
        static void Main(string[] args)
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\My Documents\Little Prince.docx”);
            Section section = document.AddSection();

            //Insert Header
            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph headerParagraph = header.AddParagraph();
            TextRange text = headerParagraph.AppendText(“Little Prince”);
            text.CharacterFormat.FontName = “Calibri”;
            text.CharacterFormat.FontSize = 14;
            text.CharacterFormat.TextColor = Color.DarkSlateGray;
                = Spire.Doc.Documents.HorizontalAlignment.Right;
                = Spire.Doc.Documents.BorderStyle.Single;
            headerParagraph.Format.Borders.Bottom.Space = 0.1F;

            //Save and Launch
            document.SaveToFile(“Header.docx”, FileFormat.Docx);


Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertHeader
    Friend Class header
        Shared Sub Main(ByVal args() As String)
            ‘Load Document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\My Documents\Little Prince.docx”)
            Dim section As Section = document.AddSection()

            ‘Insert Header
            Dim header As HeaderFooter = section.HeadersFooters.Header
            Dim headerParagraph As Paragraph = header.AddParagraph()
            Dim text As TextRange = headerParagraph.AppendText(“Little Prince”)
            text.CharacterFormat.FontName = “Calibri”
            text.CharacterFormat.FontSize = 14
            text.CharacterFormat.TextColor = Color.DarkSlateGray
            headerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
            headerParagraph.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single
            headerParagraph.Format.Borders.Bottom.Space = 0.1F

            ‘Save and Launch
            document.SaveToFile(“Header.docx”, FileFormat.Docx)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Download Spire.Doc for .NET Here


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s