HeaderFooter IsLinkedToPrevious

Hello,

I’m trying to get the headerFooter settings of a section. The problem I face is that when a headerFooter is linked to the one of the previous section, it is null when I try to get it. Instead, I should retrieve an headerFooter element with its IsLinkedToPrevious proerty set to null. This is not the case.

For example, if you try to get the first page footer of the second section of the attached document, you’ll have a null reference instead:

Console.WriteLine(doc.Sections[1].HeadersFooters[HeaderFooterType.FooterFirst] != null);

Is there any way to know if a header or a footer is linked to previous?

Kind regards,

Hello

Thank you for reporting this problem to us. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed.
Best regards,

Hello,

Thank you for your answer.

Until you fix this problem, is there a workaround that would allow me to know if a header is linked to the one of its previous section?

Regards,

Hello

Thanks for your inquiry. You can try using the following code:

Document doc = new Document("Test.doc");
HeaderFooter footer = doc.Sections[1].HeadersFooters[HeaderFooterType.FooterFirst];
if (footer == null)
{
    footer = new HeaderFooter(doc, HeaderFooterType.FooterFirst);
    doc.Sections[1].HeadersFooters.Add(footer);
    Console.WriteLine(footer.IsLinkedToPrevious);
}
doc.Save("out.doc");

Best regards,

Hello,

Thank you for your answer. Being able to determine whether or not a headerFooter is linked to the previous is very important to us.

Do you think this code fixes the bug?

public static void FixHeaderFooterLinkedWithPreviousBug(Document wordDoc)
{
    Array headerFooterTypes = Enum.GetValues(typeof(HeaderFooterType));
    Section previousSection = wordDoc.FirstSection;
    while (previousSection.NextSibling != null)
    {
        Section currentSection = (Section) previousSection.NextSibling;
        if (previousSection.HeadersFooters.Count != 0)
        {
            foreach(HeaderFooterType headerFooterType in headerFooterTypes)
            {
                if (previousSection.HeadersFooters[headerFooterType] != null)
                {
                    if (currentSection.HeadersFooters[headerFooterType] == null)
                    {
                        currentSection.HeadersFooters.Add(new HeaderFooter(wordDoc, headerFooterType));
                    }
                }
            }
        }
        previousSection = currentSection;
    }
}

Best regards,

Hello

Thanks for your inquiry. Yes, I think your code should work this problem around.
Please let me know in case of any issues. I will be glad to help you.
Best regards,

Hello,

It seems to me the bug you have is more general. Looks like Aspose.Words returns null every time a header or a footer doesn’t have content.

In the attached document, this is problematic to us since we are not able to determine that the document has both a primary header and an empty first header.

Here is the code to reproduce this problem ?

Document document = new Document(@"header test.doc");
HeaderFooterCollection headersFooters = document.FirstSection.HeadersFooters;
foreach (HeaderFooter headerFooter in headersFooters)
{
    Console.WriteLine(headerFooter.HeaderFooterType);
}

Until you provide a fix, do you have a workaround?

Best regards,

Hi David,
Thanks for your request. This is expected that the section does not contain HeaderFooter nodes of all types. Ms Word simply does not write HeaderFooter if it is empty in this case Aspose.Words returns null for this type of HeaderFooter. As a workaround, you can simply check whether the section contain HeaderFooter nodes of each type:
https://reference.aspose.com/words/net/aspose.words/headerfootertype/
Best regards,

Hello,

Thank you for your answer.

In the case a document as both a primary section and an empty first section, how can I determine this programmatically ? If I do a check of a section which has a primary header, I cannot know if the section doesn’t have a first header (i.e. all the pages will have the primary header) or has an empty first header (i.e. all the pages will have the primary header except the first page).

I hope I’m clear enough.

Best regards,

Hi
Thanks for your request. I suppose, you can use code like the following to check each type of HeaderFooter in each section:

// Open document.
Document doc = new Document("in.doc");
// Loop through section in the document.
foreach(Section sect in doc.Sections)
{
    // Process HeaderFooter nodes in order you need.
    if (sect.HeadersFooters[HeaderFooterType.HeaderFirst] != null)
    {
        // Process first page header.
    }
    if (sect.HeadersFooters[HeaderFooterType.HeaderEven] != null)
    {
        // Process even page header.
    }
    if (sect.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)
    {
        // Process primary page header.
    }
    if (sect.HeadersFooters[HeaderFooterType.FooterFirst] != null)
    {
        // Process first page footer.
    }
    if (sect.HeadersFooters[HeaderFooterType.FooterEven] != null)
    {
        // Process even page footer.
    }
    if (sect.HeadersFooters[HeaderFooterType.FooterPrimary] != null)
    {
        // Process primary page footer.
    }
}

Also, I think, you should check DifferentFirstPageHeaderFooter and OddAndEvenPagesHeaderFooter properties:
Best regards,