We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Footer in the LAST PAGE of a Dynamic Document alone

I have a dynamic word document already with different headers and footers. It can have as many pages as the user wants making it a dynamic document. I want the last page of that document alone to have a separate footer which I am not able to achieve. Please let me know the possibilities.

Thanks in advance

Hi Jacob,

Thanks for your inquiry. There is no direct way to define header/footer of the last page in MS Word documents. However, there are two workarounds of this issue.

1. Use separate section of the last page. In this case, you can specify header/footer for the each section.

Aspose.Words uses our own Rendering Engine to layout documents into pages. The Aspose.Words.Layout namespace provides
classes that allow to access information such as on what page and where
on a page particular document elements are positioned, when the document
is formatted into pages. Please read about LayoutCollector and
LayoutEnumerator from here:

Please use the
LayoutCollector.getStartPageIndex method to get 1-based index of the
page where node begins. Please find the last node of second last page of document using getStartPageIndex method and insert section break of type new page at the position of that node.

2. Use IF field conditions to display different text on the last page. For example see the following code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Build document.

// Just for testing add few pages.

for (int i = 0; i < 10; i++)


// Move DocumentBuilder cursor to the primary footer.


// There is no direct way to have different headers/footers for the last page.

// However, we can achieve this using IF field.

// In our case, IF field code will look like the following:

// { IF "{PAGE}" = "{NUMPAGES}" "This is last page" "This is primary footer" }

// Insert the first part of the IF field

Field field = builder.insertField("IF \"", null);

// Move DocumentBuilder cursor to the next run after field start.


// Insert PAGE field.

builder.insertField("PAGE", null);

// Insert operator.

builder.write("\" = \"");

// Insert NUMPAGES field.

builder.insertField("NUMPAGES", null);

// Insert true/false values.

builder.write("\" \"This is last page\" \"This is primary footer\"");

// Save output document.

doc.save(MyDir + "out.docx");

<!–[if gte mso 9]>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}


Thank you so much for the reply. It was actually working well. And now when I restarted numbering in that particular section, the last page of the section doesn’t equal the total number of pages. So the last page footer isn’t displayed. Can you help with this?

Thanks in advance,

Hi Jacob,

Thanks for your inquiry.
In this case, I suggest you please use separate section of the last page. In this case, you can specify header/footer for the each section.

If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.