Headers and Footers
This guide will show you how to add your Word document headers and footers, as well as create formatting rules for their appearance.
Before you begin
You should get your API key from your UniCloud account.
If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.
In this repository examples, you can discover multiple instances of using headers and footers examples. However, for this specific illustration, a comprehensive amalgamation of these concepts has been created.
How it works
package main
import (
"fmt"
"log"
"github.com/unidoc/unioffice/common"
"github.com/unidoc/unioffice/common/license"
"github.com/unidoc/unioffice/document"
"github.com/unidoc/unioffice/measurement"
"github.com/unidoc/unioffice/schema/soo/ofc/sharedTypes"
"github.com/unidoc/unioffice/schema/soo/wml"
)
func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
func main() {
doc := document.New()
defer doc.Close()
// Adding content
for i := 0; i < 25; i++ {
doc.AddParagraph().AddRun().AddText(text)
}
// Getting the image
img, err := common.ImageFromFile("gophercolor.png")
if err != nil {
log.Fatalf("unable to create image: %s", err)
}
// Checking if there is a header
hdr, ok := doc.BodySection().GetHeader(wml.ST_HdrFtrDefault)
if !ok {
// If not, creating it
hdr = doc.AddHeader()
doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)
}
// Main header with an image
hdrPara := hdr.AddParagraph()
hdrRun := hdrPara.AddRun()
hdrRun.AddText("Main Document Title")
hdrRun.AddBreak()
iref, err := hdr.AddImage(img)
if err != nil {
log.Fatalf("unable to add image to header: %s", err)
}
imgInl, _ := hdrPara.AddRun().AddDrawingInline(iref)
imgInl.SetSize(0.5*measurement.Inch, 0.5*measurement.Inch)
// Even header
evenHdr := doc.AddHeader()
evenHdr.AddParagraph().AddRun().AddText("Even Header")
doc.BodySection().SetHeader(evenHdr, wml.ST_HdrFtrEven)
// Odd header
oddHdr := doc.AddHeader()
oddHdr.AddParagraph().AddRun().AddText("Odd Header")
doc.BodySection().SetHeader(oddHdr, wml.ST_HdrFtrDefault)
// Set EvenAndOddHeaders flag
boolTrue := true
doc.Settings.X().EvenAndOddHeaders = &wml.CT_OnOff{
ValAttr: &sharedTypes.ST_OnOff{Bool: &boolTrue},
}
// Add Footer
ftr := doc.AddFooter()
ftrPara := ftr.AddParagraph()
ftrPara.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone)
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)
doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault)
doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrEven)
// Save the file
if err := doc.SaveToFile("combined-header-footer.docx"); err != nil {
fmt.Println(err)
}
}
The necessary packages are imported in lines 3-13
, including the UniOffice packages for document manipulation.
The init function in lines 15-22
authenticates your request using your UNIDOC_LICENSE_API_KEY.
The main functionality begins in the main function in lines 26-96
. It initializes a new document, populates it with repetitive content in lines 27-33
, and reads an image in lines 35-39
.
It checks for the existence of the default header in the document’s body section in lines 41-47
. If the default header does not exist, it is created. Within this header, a paragraph with a title and a line break is formed, followed by an inline-inserted image.
Furthermore, two additional headers are generated and added. One header is set as the default for odd-numbered pages, and the other exclusively for even-numbered pages, in lines 62-70
. This configuration enables alternating headers and footers.
The EvenAndOddHeaders flag is enabled by setting it to true, allowing the alternating headers and footers to take effect.
A footer is appended, featuring a paragraph with a right-aligned tab stop, as well as dynamic fields that display a subtitle and page numbers in lines 78-90
.
Finally, the document is saved as “combined-header-footer.docx” in lines 92-95
.
Run the code
Save the code in a file called main.go
and run the command to generate a word document with different headers and footers.
go run main.go
Sample output
Open the output word document with Word and you will see the headers and footers that we just created.