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
Jacob
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:
http://www.aspose.com/docs/display/wordsjava/LayoutCollector+class
http://www.aspose.com/docs/display/wordsjava/LayoutEnumerator+class
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++)
builder.insertBreak(BreakType.PAGE_BREAK);
// Move DocumentBuilder cursor to the primary footer.
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
// 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.
builder.moveTo(field.getStart().getNextSibling().getNextSibling());
// 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]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:DoNotShowPropertyChanges/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>AR-SA</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<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”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>
<![endif]–>
Hi,
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,
Jacob
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
like.
us we’ll start our investigation into your issue.