Free Support Forum -

How to add page Number. in Aspose.Word


I am Exporting MSWORD from HTML Stream using Aspsoe.Word .

I use builder.InsertHTML() to which I pass HTML stream . To add page break I use
this "


" which works fine it adds page break and all that …

Now I wan to add Header and Footer and in Footer I want to show page number.

at some point I want to reset page no. to 1 again

How to achieve this ?

Regards ,
Jeevan Joshi.

Hi Jeevan,

Thanks for your inquiry. Please note that you can not insert header/footer by using DocumentBuilder.InsertHtml method.

The HeaderFooter class represents a container for the header or footer text of a section. HeaderFooter is a section-level node and can only be a child of Section. So, header and footer repeat on each page of a section. There can only be one HeaderFooter or each HeaderFooterType in a Section.

You can insert header/footer after calling DocumentBuilder.InsertHtml method and insert page number field in footer of a section as shown in following code snippet.

Document doc = new

DocumentBuilder builder = new DocumentBuilder(doc);



Paragraph right

" +

"Implicit paragraph left" +


Div center
" +


Heading 1 left.


InsertHeaderFooter(builder.CurrentSection, HeaderFooterType.FooterPrimary);

InsertHeaderFooter(builder.CurrentSection, HeaderFooterType.HeaderFirst);


//Set start number

builder.PageSetup.PageStartingNumber = 1;

//Page numbers will be restarted for each section

builder.PageSetup.RestartPageNumbering = true;

//Insert PAGE field into the header


builder.Write("Page ");

builder.InsertField("PAGE", string.Empty);


//Insert page break


//Insert section break


doc.Save(MyDir + "Out.docx");

private static void InsertHeaderFooter(Section sect, HeaderFooterType headerType)


HeaderFooter header = sect.HeadersFooters[headerType];

if (header == null)


// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(sect.Document, headerType);




Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section. Please read following documentation link for your kind reference.

Hope this helps you. Please let us know if you have any more queries.