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 page number in Word document as well as page headers. 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 from the rest of the pages:

Insert page number
Insert header

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:

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
string MyAppKey = ""; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = ""; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
StorageApi storageApi = new StorageApi(MyAppKey, MyAppSid);
string fileName = "Sample-Document.docx";
// Upload source document to Cloud Storage
PutCreateRequest request = new PutCreateRequest(fileName, File.OpenRead(@"c:\Data\" + fileName), null, null);
storageApi.PutCreate(request);

Insert Header in a Word Document

Insert Header Footer API adds headers in a document and its headerFooterType parameter can take one of the following six values:

  • HeaderFirst - Header for the first page of the section.
  • HeaderPrimary - Primary header, also used for odd numbered pages.
  • HeaderEven - Header for even numbered pages.
  • FooterFirst - Footer for the first page of the section.
  • FooterPrimary - Primary footer, also used for odd numbered pages.
  • FooterEven - Footer for even numbered pages.

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

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
string MyAppKey = ""; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = ""; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
string fileName = "Sample-Document.docx";
string folder = null; // File exists at the root of the storage
// Insert Header for the first page
var putHeaderFooterRequest = new PutHeaderFooterRequest(fileName, "HeaderFirst", folder);
var actual = wordsApi.PutHeaderFooter(putHeaderFooterRequest);

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

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
// Insert Header for all other pages
var putHeaderFooterRequest = new PutHeaderFooterRequest(fileName, "HeaderPrimary", folder);
var actual = wordsApi.PutHeaderFooter(putHeaderFooterRequest);

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

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
var sectionIndex = 0;
var body = new PageSetup
{
DifferentFirstPageHeaderFooter = true
};
var pageSetupRequest = new UpdateSectionPageSetupRequest(fileName, sectionIndex, body);
var actual = wordsApi.UpdateSectionPageSetup(pageSetupRequest);

After the execution of above code snippet, empty paragraphs have been added to the headers of the document. Now, in oder to add text to these headers, we will use Insert Paragraph API. The code shown below is adding “Aspose” text in the first header and “Cloud File Format APIs” text in all other headers:

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
var run = new Run { Text = "ASPOSE" };
var runRequest = new PutRunRequest(fileName, "sections/0/headersfooters/1/paragraphs/0", run);
var actual = wordsApi.PutRun(runRequest);

Apply Formatting to a Header’s Text

Formatting can be applied to a header’s text using Update Run Font API. The API accepts fontDto object in its body, whose Resource Properties’ details are given in Update font properties for text in a Word document article. The following code is setting font family of the header text to Verdana, text size to 14 and text color to black.

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
var runIndex = 0;
var fontDto = new Font { Bold = true, Name = "Verdana", Size = 14, Color = new XmlColor { Web = "#000000" } };
// Apply formatting to first page header
var documentParagraphRunFontRequest = new PostDocumentParagraphRunFontRequest(fileName, fontDto, "sections/0/headersfooters/1/paragraphs/0", runIndex);
var actual = wordsApi.PostDocumentParagraphRunFont(documentParagraphRunFontRequest);
// Apply formatting to header of all other pages
documentParagraphRunFontRequest = new PostDocumentParagraphRunFontRequest(fileName, fontDto, "sections/0/headersfooters/0/paragraphs/0", runIndex);
actual = wordsApi.PostDocumentParagraphRunFont(documentParagraphRunFontRequest);

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

Insert Page Numbers in a Word Document

InsertPageNumbers API is used to insert page numbers in a Word document. The API accepts the 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.

// For a complete example and data file, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet/blob/master/Examples/Aspose.Words.Cloud.Sdk.Examples/HeaderFooter/InsertHeadersAndPageNumbersToAWordDocument.cs
var body = new PageNumber { Alignment = "right", Format = "{PAGE} of {NUMPAGES}", IsTop = true, SetPageNumberOnFirstPage = true };
var insertPageNumbersRequest = new PostInsertPageNumbersRequest(fileName, body);
var actual = wordsApi.PostInsertPageNumbers(insertPageNumbersRequest);

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

Suggested Post:

We also recommend reviewing the following blog posts to learn more about: