When I append attachments to my document then the Headers and Footers are applied to the appended document even though I used the IgnoreHeaderFooter format option.
Document input = new Document(filePath)
var output = new Document();
output.RemoveAllChildren();
output.AppendDocument(input, ImportFormatMode.KeepSourceFormatting);
Document attachmentDoc = new Document(tempFilePath);
ImportFormatOptions options = new ImportFormatOptions() { IgnoreHeaderFooter = true, KeepSourceNumbering = false };
output.AppendDocument(attachmentDoc, ImportFormatMode.KeepSourceFormatting, options);
@neilbyrd
-
The IgnoreHeaderFooter property in ImportFormatOptions is designed to prevent headers and footers from being imported when appending a document using AppendDocument in Aspose.Words for .NET . This setting is documented as part of the API for controlling header/footer behavior during import operations .
-
Your code correctly sets IgnoreHeaderFooter = true when calling AppendDocument, which should, in theory, exclude headers and footers from the appended document . Similar usage patterns have been discussed in community forums, confirming the intended use of this option .
-
Despite this, headers and footers are still being imported, which suggests the IgnoreHeaderFooter option may not be functioning as expected in your case. This behavior has been reported by users and may indicate a limitation or bug in the current version of Aspose.Words.
As a workaround, you can manually remove headers and footers from the source document before appending:
Document attachmentDoc = new Document(tempFilePath);
foreach (Section section in attachmentDoc.Sections)
{
section.ClearHeadersFooters();
}
output.AppendDocument(attachmentDoc, ImportFormatMode.KeepSourceFormatting);
The Section.ClearHeadersFooters() method removes all header and footer content from each section, ensuring they are not carried over during the append operation .
We recommend applying this workaround until a fix is confirmed. Please monitor this thread for further updates.
Thank you but that work around does not remove the headers and footers. The headers and footers are still present in the attachment
@neilbyrd
-
You are correct in using the IgnoreHeaderFooter = true property within ImportFormatOptions when calling AppendDocument, as this is the intended method to prevent headers and footers from being imported from the source document during appending in Aspose.Words for .NET .
-
However, despite setting this option, you’ve observed that headers and footers are still being carried over into the output document. This behavior has been previously reported by other users and may indicate a bug or limitation in the current version of Aspose.Words, where IgnoreHeaderFooter does not function as expected in certain scenarios .
-
As a workaround, manually clearing headers and footers using Section.ClearHeadersFooters() on each section of the source document before appending was suggested . However, you have confirmed that this approach also did not remove the headers and footers, which suggests the issue may be related to section-level linking or formatting inheritance .
-
Another approach discussed in similar scenarios involves inserting a section break before appending and unlinking the headers and footers of the new section to prevent inheritance from previous sections . You could try preparing the output document as follows:
// Ensure the output document has at least one section
output.EnsureMinimum();
// Insert a continuous section break to start a new section
Section newSection = new Section(output);
output.AppendChild(newSection);
// Unlink headers and footers in the new section
foreach (HeaderFooter headerFooter in newSection.HeadersFooters)
{
headerFooter.IsLinkedToPrevious = false;
}
Then append the document using AppendDocument with IgnoreHeaderFooter = true.
Since both the IgnoreHeaderFooter option and ClearHeadersFooters() have not resolved the issue in your case, this may indicate a more complex interaction between section properties or a product-level limitation. This behavior will be reviewed for further investigation.
Please continue to monitor this thread for updates or a confirmed resolution.
@neilbyrd,
I am moving your thread to the appropriate category, where one of our colleagues from the Aspose.Words team will review your issue and provide assistance soon.
@neilbyrd Could you please attach your problematic input and output documents here for testing? We will check the issue and provide you more information.
HeaderFooterIssue.zip (12.4 KB)
Thank you - attached is a zip containing the files and the code.
@neilbyrd Please try unlinking headers/footers before appending the sub-documents to the main document. Please modify your code like this:
Document input = new Document(filePath);
var output = new Document();
output.RemoveAllChildren();
output.AppendDocument(input, ImportFormatMode.KeepSourceFormatting);
Document attachmentDoc = new Document(tempFilePath);
attachmentDoc.FirstSection.HeadersFooters.LinkToPrevious(false); // <-- unlink headers/footers
ImportFormatOptions options = new ImportFormatOptions() { IgnoreHeaderFooter = true, KeepSourceNumbering = false };
output.AppendDocument(attachmentDoc, ImportFormatMode.KeepSourceFormatting, options);
That works! Thank you so much!
Unlinking the headers/footers before appending resolves the issue.
1 Like