Insert Headers and Page Numbers in a Word Document

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

The header is a section of the document that appears in the top margin and generally contains information such as the page number, date, and document name. With Aspose.Words REST APIs you can insert headers and page numbers in a Word document. By default, the headers are the same on each page but we can create different headers for odd and even pages.

This post aims to achieve the following layout of the document in which the header on the first page is different than the rest of the pages:

Before calling any Aspose REST API, we need to create Aspose Cloud account and obtain our App Key and App SID. Getting Started article explains how to perform these steps.

Moreover, we need to upload the sample document to the Cloud Storage as all the operations on the document will be performed in the Cloud:

Insert Header in a Word Document

PUT /words/{name}/{sectionPath}/headersfooters API adds headers in a document and its headerFooterType parameter can take one of the following six values:

HeaderFirstHeader for the first page of the section.
HeaderPrimaryPrimary header, also used for odd numbered pages.
HeaderEvenHeader for even numbered pages.
FooterFirstFooter for the first page of the section.
FooterPrimaryPrimary footer, also used for odd numbered pages.
FooterEvenFooter for even numbered pages.

Please use HeaderFirst value to insert header in the first page:

and HeaderPrimary value to insert header in the rest of the pages:

Since the first header is different than the others, set DifferentFirstPageHeaderFooter parameter value to true as shown below:

So far empty paragraphs have been added to the headers of the document. In oder to add text to these headers we will use PUT /words/{name}/{paragraphPath}/runs API. The code shown below is adding “Aspose” text in the first header and “Cloud File Format APIs” text in all other headers:

Apply Formatting to a Header’s Text

Formatting can be applied to a header’s text using POST /words/{name}/{paragraphPath}/runs/{index}/font API. The API accepts fontDto object in its body, whose Resource Properties’ details are given in Update Font Properties of the Text article. The following code is setting font family of the header text to Verdana, text size to 14 and text colour to black.

After making the above API calls, the input document looks as follows:

Insert Page Numbers in a Word Document

POST /words/{name}/insertPageNumber API is used to insert page numbers in a Word document. The API accepts following JSON object in its body:

  "Format": "string",
  "Alignment": "string",
  "IsTop": true,
  "SetPageNumberOnFirstPage": true

Since we are adding page numbers in the header, we will set IsTop parameter’s value to true. However, to add page numbers in the footer, set the value to false.

Finally, we have added headers and page numbers to the document.

The complete code of this post is available on GitHub.