How to add a page number to the footer in a Word DOCX document in Java

Today is your lucky day. That is because we are going to be achieving our goal of adding DOCX page numbers in a matter of mere minutes, as opposed the the hours that it would normally take to implement programmatically. We are going to employ an API that will get us the results we need after minimal setup and very little required code.

Let’s add a reference first to our respositories.

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then our dependencies.

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

And finally, call our DOCX page number function.

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.EditDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");EditDocumentApi apiInstance = new EditDocumentApi();DocxSetFooterAddPageNumberRequest reqConfig = new DocxSetFooterAddPageNumberRequest(); // DocxSetFooterAddPageNumberRequest | Document input requesttry {DocxSetFooterResponse result = apiInstance.editDocumentDocxSetFooterAddPageNumber(reqConfig);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditDocumentApi#editDocumentDocxSetFooterAddPageNumber");e.printStackTrace();}

Super easy.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.