Aspose.Words

Controlling How Header and Footers Appear

Continuing Headers and Footers from the Destination Document

The headers and footers of a document provide an option which allows the current section’s headers and footers to continue on from the previous section. This setting can be seen in Microsoft Word below.

In Aspose.Words this setting is controlled by the HeaderFooterCollection.LinkToPrevious method.  Passing a value of true will cause all types of headers footers to removed from this section if there are any and the headers and footers from the previous section to be displayed instead.

Example AppendDocument_LinkHeadersFooters

Shows how to append a document to another document and continue headers and footers from the destination document.

[Java]

 

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");

Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

 

// Set the appended document to appear on a new page.

srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

 

// Link the headers and footers in the source document to the previous section.

// This will override any headers or footers already found in the source document.

srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(true);

 

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.save(gDataDir + "TestFile.LinkHeadersFooters Out.doc");

 

 

If the source document has multiple sections already all using the same headers and footers then they will all most likely be linked to the headers and footers of the previous section. This will mean after the headers and footers of the first section are linked to the previous section these sections will also automatically inherit the headers and footers from destination document as well.

In some cases if your source document uses different headers in multiple sections you may need to call the HeaderFooterCollection.LinkToPrevious method on each of these sections in order for them to inherit the headers and footers from the destination document.

The resulting document is displayed below. The source document now takes on the headers and footers of the destination document.

Stopping Headers and Footers from Continuing from the Destination Document

As described previously a section may be already set to inherit the headers and footers from the previous section. Even a document which has no content in the headers and footers can still have a link to the headers and footers of the previous section. When such a document is appended to another document then the headers and footers from the destination document will carry through to the source document.

To avoid this situation the headers and footers must be unlinked by calling the HeaderFooterCollection.LinkToPrevious method on the first section of the source document. Passing false to this method will unlink all types of headers and footers from the previous section. It is enough to unlink only the first section as any further linked sections in the source document now will not inherit any headers or footers from the previous section.

Example AppendDocument_UnlinkHeadersFooters

Shows how to append a document to another document so headers and footers do not continue from the destination document.

[Java]

 

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");

Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

 

// Even a document with no headers or footers can still have the LinkToPrevious setting set to true.

// Unlink the headers and footers in the source document to stop this from continuing the headers and footers

// from the destination document.

srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);

 

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.save(gDataDir + "TestFile.UnlinkHeadersFooters Out.doc");

 

 

Removing Headers and Footers from the Source Document

Sometimes documents which are being joined are no longer required to display their headers and footers. Removing them can be easily achieved by calling the Section.ClearHeadersFooters method.

Example AppendDocument_RemoveSourceHeadersFooters

Shows how to remove headers and footers from a document before appending it to another document.

[Java]

 

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");

Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

 

// Remove the headers and footers from each of the sections in the source document.

for (Section section : srcDoc.getSections())

{

    section.clearHeadersFooters();

}

 

// Even after the headers and footers are cleared from the source document, the "LinkToPrevious" setting

// for HeadersFooters can still be set. This will cause the headers and footers to continue from the destination

// document. This should set to false to avoid this behaviour.

srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);

 

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.save(gDataDir + "TestFile.RemoveSourceHeadersFooters Out.doc");

 

 

As in the previous examples above the headers and footers are unlinked from the previous section to avoid the destination headers and footers being used in place of the removed headers.

The result shows the joined document retains the headers and footers the destination portion but are removed in the source portion of the document.