I want to retrieve the content of headers and footers for every section of a document.
For the attached document, could you tell me how to retrieve the content of the first page header of the second section? This header is actually linked to the one of the previous section. But as you can see in the sample below, I cannot access this infomation programmatically since the returned headerfooter object is null.
var doc = new Document("HF.docx");
Section secondSection = doc.Sections[1];
if (secondSection.PageSetup.DifferentFirstPageHeaderFooter)
{
HeaderFooter header = secondSection.HeadersFooters[HeaderFooterType.HeaderFirst];
}
Thanks for your query. Please see the attached images for header/footer details of your document. If second section is Linked To Previous (see HF-linkToPreviour.png) then there is no HeaderFirst in second section.
Please use the following code snippet to get header and footer of second sections.
What I actually want is a generic way to get the different header/footer content of each section as they appear in Microsoft Word, no matter whether they are linked to the previous section of not. And so far, I was unable to do so.
According to my understanding, if a section has a header/footer that is linked to the one of the previous section, the section should have a HeaderFooter object with:
- The property IsLinkedToPrevious returning true
- No child nodes.
Maybe you could ask an Aspose.Words developer for his point of view?
Otherwise, how can I retrieve the “visible” content of the headers and footers of each section?
Please not that Aspose.Words simply reads the information present in the document and based on this information it builds the DOM in memory. Since DOCX is a XML based format, I have changed the extension of the DOCX document to .zip and attached a couple of zip archives here for your reference.
When you enable ‘Link to Previous’ option in MS Word or by using Aspose.Words, the Header and Footer in the current Section contains the same content as in the previous Section and the Header/footer of the current Sections are removed because they are no longer in use. This is by MS Word design and Aspose.Words does the right thing.
To confirm, please open or extract ‘HF - Linked to previous.zip’ file. Navigate to ‘word’ folder. There you’ll find ‘header1.xml’, ‘header2.xml’, ‘header3.xml’ and ‘header4.xml’ (you can verify these four headers in DocumentExplorer as well) and similarly there are four xml files for Footers. The remaining document Headers and Footers got removed which you can find in ‘HF - Not linked to previous.zip’.
May be you can work around this problem by first temporarily turning the ‘link to previous’ option off in each Section by using HeaderFooter.IsLinkedToPrevious property. After that you can loop through all headers/footers in your document.
You might find the following code helps to easily retrieve the content from any linked headers or footers. Simply call the method with your document object before doing your retrieval then you can retrieve the content from the linked header or footer just like you are trying to do now.
The code works by copying linked header footers from previous sections to the actual section.
private static void CopyLinkedHeadersFooters(Document doc)
{
foreach (Section section in doc.Sections)
{
if (section == doc.FirstSection) continue;
var previousSection = (Section)section.PreviousSibling;
var previousHeaderFooters = previousSection.HeadersFooters;
foreach (HeaderFooter headerFooter in previousHeaderFooters)
{
if (section.HeadersFooters[headerFooter.HeaderFooterType] != null) continue;
if (
(headerFooter.HeaderFooterType == HeaderFooterType.HeaderFirst
|| headerFooter.HeaderFooterType == HeaderFooterType.FooterFirst)
&&
(previousSection.PageSetup.DifferentFirstPageHeaderFooter != section.PageSetup.DifferentFirstPageHeaderFooter)
) continue;
var newHeaderFooter = (HeaderFooter)previousHeaderFooters[headerFooter.HeaderFooterType].Clone(true);
section.HeadersFooters.Add(newHeaderFooter);
}
}
}
Thank you for your answers. I was finally able to do what I wanted.
I took inspiration from the code provided by Adam. Handling the first and even headers and footers was actually a bit more complex since the section that contains the header content is not always the previous one.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.