A quick view of MS Word document transformation.

A quick view of MS Word document transformation.

A header is the top margin of each page, and a footer is the bottom margin of each page. Headers and footers are useful for including information that you want to display on every page of a document such as your name, dates, the title of the document, page numbers, or disclaimers to documents. When using MS Word, it provides the capabilities to add Header and Footers based on predefined layouts or enable you to add custom headers and footers. Most built-in headers contain some placeholder text and either, you can keep the placeholder text or replace it with your own data feed. Also, when updating the information in the Header or Footer section, the content inside the main body of the document is grayed out which indicates that these objects are totally isolated from the rest of the page content and the operation which you perform on these objects is specific to these areas. In this article, we are going to learn the steps on how to add or update the header footer in Word Document using REST API.

Word processing API

Aspose.Words Cloud API provides the capabilities to load MS Word files from Cloud storage, as well as it allows the user to pass the input file directly in the request body and the API returns the updated file in the response object. It provides the features to Read, Add, Update or Delete specific or all Header and Footer objects. Furthermore, you may also specify to render different headers/footers for the first page and for odd/even pages.

Furthermore, the API also enables you to set formatting information such as font and paragraph properties for the header/footer text. You also get the leverage to insert dynamic page numbers into the header/footer section. Last but not the least, you may consider using table objects to make one part of the header/footer text-align to the left edge and the other to the right edge.

Scope of Article

Our Aspose.Words Cloud SDK for .NET allows you to work with Aspose.Words Cloud REST APIs quickly and easily, as it takes care of a lot of low-level details regarding making requests and handling responses and lets you focus on writing code specific to your business requirements. In this article, we are going to use Aspose.Words Cloud SDK for .NET in Visual Studio for Mac. This post defines the steps to insert Header Footer in Word Document where custom formatting will be defined. Then the formatting of exiting Footer content is updated and PageNumber information is inserted at the Bottom-Right position. All these operations will be performed in the cloud.

Installation

In order to get started, first, you need to install Visual Studio on your system. Create a sample solution and add NuGet reference of Aspose.Words Cloud SDK for .NET. So in order to add a reference, right-click on the solution and select Manage NuGet Packages… menu option. Enter Aspose.Words-Cloud in the search text field. Select the option and click the Add package button.

Now Aspose.Words.Cloud.Sdk reference appears in the solution. There are some benefits of using cloud APIs in contrast to on-premise (aspose.com) APIs. The reason is, you do not need to manually keep track and update the API versions and you do not need to pay for the whole license.

Aspose.Cloud Dashboard

Simply create an account on Aspose.Cloud dashboard or signup through your existing Google or GitHub account and start using our Cloud APIs. Another amazing feature of Cloud APIs is that you can use them on any platform using any supported language.

As indicated above, the article emphasizes options regarding the addition and update of Header and Footer objects inside MS Word documents. For the sake of testing, we have used an existing MS Word document template with the name Business Letter. The document needs to be uploaded to Aspose cloud storage and all the manipulation and document processing will be performed over the cloud. Once all processing is completed, we can also download the resultant file copy from the Cloud storage.

using Aspose.Words.Cloud.Sdk;
using Aspose.Words.Cloud.Sdk.Model;
using Aspose.Words.Cloud.Sdk.Model.Requests;

Now in order to use Aspose.Cloud APIs, you need to use App Key and App SID information in your code. These keys are comprised of Alphanumeric digits. These are unique codes associated with your subscription account. In order to get these details, please click the My Apps tab on Aspose.Cloud dashboard. Once we have the Keys, we need to initialize Aspose.Words API object.

string MyAppKey = "f6axxxxxxxxxxxxxxxxxx";     // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = "478e4827-xxxxx-xxxx-xxxx-xxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
// create an object of WordsApi while passing AppKey and AppSid information
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);

Insert Header object

In order to add the header object inside the MS Word file, we need to perform the following operations in sequence.

  1. Create an object of InsertHeaderFooterRequest where we pass HeaderFooterLink type as an argument.
  2. Insert HeaderFooterRequest object to WordsApi object using InsertHeaderFooter (…) method.
  3. Create a Run object which holds sample Text information.
  4. Create InsertRunRequest object where we pass the reference of paragraph(SectionPath) in HeaderFooter object, where Run text needs to be added.
    Then call InsertRun (…) method of WordsApi, which adds the RunRequest to HeaderFooter object.

The HeaderFooter.TypeEnum can be either of the following values.

Image showing preview of recently added Header

Image showing preview of recently added Header

Update Text formatting of Newly inserted Header

Once the Header object is added, we can update the text formatting of the content inside it. In order to fulfill this requirement, we need to perform the following steps.

  1. Create an object of Font class, where we specify Font name, size, color information.
  2. In order to make the text more presentable, mark Engrave property as True.
  3. Create an object of UpdateRunFontRequest class where we pass the input file, Font, SectionPath and runIndex as an argument.
  4. Finally, use UpdateRunFont(..) method and add UpdateRunFontRequest object to WordsApi instance.
Image showing preview of Header after text formatting has been applied.

Image showing preview of Header after text formatting has been applied.

The API also offers the feature to update the formatting of content inside the existing Header / Footer object. In order to fulfill this requirement, we can use the above-specified code lines but the only difference is the change of headerfooter index to 2. See the following code line.

// Apply formatting to first paragraph of HeaderFooter object
var FooterDocumentParagraphRunFontRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.UpdateRunFontRequest("BusinessLetter.docx", 
FooterFontDto, "sections/0/headersfooters/2/paragraphs/0", FooterrunIndex);
Image displaying the change in Footer text formatting.

Image displaying the change in Footer text formatting.

Page numbers provide very useful information such as the current page and the total number of pages in a document. The following steps define how to accomplish the requirements.

  1. First create an object of PageNumber class, where we define, Alignment of text, the format of the text, location to render PageNumber information and also, specify if PageNumber needs to be rendered on the first page or not.
  2. Create InsertPageNumbersRequest object and pass PageNumber object as an argument.
  3. Finally, call InsertPageNumbers(..) method to add InsertPageNumbersRequest to WordsApi instance.
Preview Page number in the footer section.

Preview Page number in the footer section.

Conclusion

In this article, we have learned the steps on how to add a Header Footer in Word Document using Aspose.Words Cloud SDK for .NET. Please note that we also have Cloud SDKs available for Java, PHP, Ruby, Python, Go, Swift, C++, Node.Js, Android. For further details, please visit Aspose.Words Cloud.

We also recommend visiting the following to learn more about