Adding Page Number Information

This guide will demonstrate how to include the current page number and the total page count in the document’s footer.

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/page-numbers folder in the unioffice-examples directory.

cd unioffice-examples/document/page-numbers

How it works

Lines 4-11 import the UniOffice packages and other required dependencies.

The init function in lines 13-20 authenticates your request with your UNIDOC_LICENSE_API_KEY.

The main function spans from line 24 to 47. It begins by creating a new document and adding a footer to it in line 28. Following that, a paragraph is added, and tab stops are configured for this paragraph.

At line 32, a run is added on this paragraph, followed by a tab and the current page using the AddFieldWithFormatting method with the FieldCurrentPage property of the document. Similarly, the same is done with the FieldNumberOfPages property, which contains the total number of pages in the document.

Finally, 20 paragraphs are added within a loop, each containing 5 runs of text to generate additional pages in the document. The document is then saved in line 46.

Run the code

Run this command to create a new 5-pages document with page numbers:

go run main.go

Sample output

Page Number 1

Page Number 2

Page Number 3

Page Number 4

Page Number 5

Got any Questions?

We're here to help you.