Header and Footer With Table
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.
Clone the project repository
In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.
git clone https://github.com/unidoc/unioffice-examples
To get the example navigate to the path document/header-footer-with-table
folder in the unioffice-examples directory.
cd unioffice-examples/document/header-footer-with-table
How it works
The example code starts by importing the necessary libraries in the import section, lines 9-19
. The init
function defined in lines 21-25
, initializes the package by setting the license API key using license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
.
The main
function is defined in lines 30-127
. First, a new document is instantiated in line 31
using document.New()
. The AddHeader
method of the Header object is used to get a document.Header
object. The code block in lines 38-65
contains the code used to add a table to the obtained header.
First, a table is created using AddTable
method of Header
object. Then the table properties are set in lines 41-46
. This includes a setting width, alignment, and borders. Then the content of the table is set in lines 49-64
. For more details on how tables and their contents are created, check this guide on table. The header is then set in line 67
using:
doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)
In line 70
, a footer is created using doc.AddFooter()
. Then another code block 73-113
is defined where the the table for the footer is created. The process is the same as the header’s table. Except, in this part there is an image content. In line 115
, the footer is set using:
doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault)
Then lines 118-121
, the content of the body is added.
Finally, the document is saved to file using doc.SaveToFile("header-footer-with-table.docx")
.
Run the code
Use the following command to run the example code inside the header-footer-with-table
folder.
go run main.go