How to Add Header and Footer in Word Using Unioffice

Headers and footers are essential elements of any professional document. Whether you’re formatting a business report, legal contract, or an academic paper, headers and footers provide structure, clarity, and consistency.

This comprehensive guide will show you how to add a header in Word, format it properly, and automate its content using UniOffice library. We’ll also explore how to edit header in Word, manage footers, and dynamically insert page numbers

Understanding Headers and Footers in Word

Before jumping into the “how,” let’s understand the basics.

What Are Headers and Footers?

  • Header: Content that appears at the top of each page (e.g., title, logo, author name)

  • Footer: Content that appears at the bottom (e.g., page number, date, confidentiality note)

They help in branding, navigation, and ensuring a uniform look across pages.

Where Headers and Footers Are Used

  • Corporate documentation

  • Legal contracts

  • Research papers

  • Marketing brochures

Headers and footers are almost mandatory in all professional documents.

Why Automate Headers and Footers with UniOffice?

While Microsoft Word allows manual editing, automating with UniOffice SDK gives you full control via code.

No more copy-paste errors or layout inconsistencies.

Benefits of Using UniOffice

  • Scalability: Create thousands of documents with custom headers/footers

  • Consistency: Enforce branding across departments

  • Efficiency: Save time on repetitive formatting tasks

If you’re new, get your API key before you begin.

You Might Also Like:

Setting Up Your Local Environment

You need Go installed on your machine and access to the UniOffice Examples.

git clone https://github.com/unidoc/unioffice-examples

cd unioffice-examples/document/header-footer/

Before running any code, load your API key with:

err := license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))

How to Create a Header in Word Using UniOffice

The code below shows you how to create a header in Word from scratch.

Step 1: Add Content to the Document

doc := document.New()
defer doc.Close()

for i := 0; i < 25; i++ {
    doc.AddParagraph().AddRun().AddText(text)
}

This adds repeated paragraphs to simulate a document with multiple pages.

Step 2: Insert an Image into the Header

img, err := common.ImageFromFile("gophercolor.png")

This loads your company logo or image.

Step 3: Create the Default Header

hdr, ok := doc.BodySection().GetHeader(wml.ST_HdrFtrDefault)
if !ok {
    hdr = doc.AddHeader()
    doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)
}

Step 4: Add Text and Image to the Header

hdrPara := hdr.AddParagraph()
hdrRun := hdrPara.AddRun()

hdrRun.AddText("Main Document Title")
hdrRun.AddBreak()

iref, \_ := hdr.AddImage(img)
hdrPara.AddRun().AddDrawingInline(iref).SetSize(0.5*measurement.Inch, 0.5*measurement.Inch)

You’ve just added a title and logo into your document’s header.

Alternating Headers and Footers for Even/Odd Pages

Professional documents often require different headers for even and odd pages. Here’s how to set that up.

Step 1: Add Even and Odd Headers

evenHdr := doc.AddHeader()
evenHdr.AddParagraph().AddRun().AddText("Even Header")

doc.BodySection().SetHeader(evenHdr, wml.ST_HdrFtrEven)

oddHdr := doc.AddHeader()
oddHdr.AddParagraph().AddRun().AddText("Odd Header")

doc.BodySection().SetHeader(oddHdr, wml.ST_HdrFtrDefault)

Step 2: Enable Alternating Headers

boolTrue := true
doc.Settings.X().EvenAndOddHeaders = &wml.CT_OnOff{
    ValAttr: &sharedTypes.ST_OnOff{Bool: &boolTrue},
}

This setting activates alternate headers and footers per page type.

Let’s dive into how to create a dynamic footer with page numbers.

ftr := doc.AddFooter()
ftrPara := ftr.AddParagraph()
ftrPara.Properties().AddTabStop(6\*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone)

Step 3: Add Dynamic Page Number Fields

ftrRun := ftrPara.AddRun()
ftrRun.AddText("Some subtitle goes here")
ftrRun.AddTab()
ftrRun.AddText("Pg ")
ftrRun.AddField(document.FieldCurrentPage)
ftrRun.AddText(" of ")
ftrRun.AddField(document.FieldNumberOfPages)

This renders a live page count in the footer like: Pg 2 of 10

Step 4: Apply to Body Section

doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault)
doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrEven)

Save and Run the Code

Save the entire code to main.go, then execute:

go run main.go

This generates a Word document named combined-header-footer.docx

Open it using Microsoft Word to see your newly formatted headers and footers in action.

How to Edit Header in Word Programmatically

Want to modify an existing header? Just locate the header element and update its contents.

Example: Update Title Text

hdrRun.ClearContent()

hdrRun.AddText("Updated Document Title")

You now know how to edit header in Word dynamically without touching Word itself

You Might Also Like:

Bonus: Insert Fields Like Author or Date

You can also insert auto-filled fields such as:

  • document.FieldDate
hdrPara.AddRun().AddField(document.FieldDate)

These update automatically when the document is opened.

Key Takeaways

  • Headers and footers bring structure to your documents

  • Automating with UniOffice ensures consistency and accuracy

  • You can define different headers for even and odd pages

  • Page numbers, logos, titles can be embedded effortlessly

  • UniOffice gives you total programmatic control over header footer in Word files