HeaderFooter never null

Hi,

If I run:

HeaderFooter headerFirst = null;
HeaderFooter headerPrimary = null;
HeaderFooter headerEven = null;

Document doc = new Document(wordDocPath + HeaderFooterFileName);
foreach (Section section in doc) {
headerFirst = section.HeadersFooters[HeaderFooterType.HeaderFirst];
headerPrimary = section.HeadersFooters[HeaderFooterType.HeaderPrimary];
headerEven = section.HeadersFooters[HeaderFooterType.HeaderEven];
}

All of the HeaderFooter objects are NOT null. Only headerPrimary has some text

From the example code I have seen, I am expecting headerFirst and headerEven to be null.
Is my expectation correct? If so, any ideas why headerFirst and headerEven is not null?

@vpopat,

Thanks for your inquiry. Please ZIP and upload your input Word document (.docx/.rtf file etc) here for testing. We will investigate the issue on our end and provide you more information.

Please find the .doc attached.
LetterHead.zip (6.0 KB)

@vpopat,

There are following three headers

  • HeaderEven - does not contain any content
  • HeaderPrimary - contains some text → Letter Head Header text
  • HeaderFirst - does not contain any content

and following three footers in your document.

  • FooterEven - does not contain any content
  • FooterPrimary - does not contain any content
  • FooterFirst - does not contain any content

So, Aspose.Words will not return any of them as NULL.

You can re-save DOC file to DOCX by using MS Word. Rename DOCX to ZIP and extract the ZIP file to a folder. Go to ~/word folder to see files for headers and footers (see screenshot). So, Aspose.Words has no issues in this case.

Thanks for you reply.
I created the document from scratch and was unable to NOT have HeaderFirst, HeaderEven and FooterFirst and FooterEven.
Do you know how to remove this from the document?

@vpopat,

There can be a maximum of only one HeaderFooter of each HeaderFooterType per Section. Talking about headers, there are of three types i.e. HeaderFirst, HeaderPrimary and HeaderEven; so up to three different headers are possible in a section (for first, even and odd pages). So, in order to remove a particular HeaderFooter from a Section, you can use the following code:

HeaderFooter hfPrimary =
doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
hfPrimary.Remove();

In case you want to remove all headers and footers from a particular section, please use the following code:

doc.FirstSection.HeadersFooters.Clear();

I am not sure you understood my question…
If I have this scenario:

  1. Create a document with Header Primary
  2. Have code that evaluates to: HeaderEven == null and HeaderFirst == null and HeaderPrimary != null

If you say there is maximum of one header per section why do I get HeaderEven != null and HeaderFirst != null and HeaderPrimary != null?

@vpopat,

The following code should turn empty headers and footers into NULL (see output 18.5.zip (2.8 KB)):

Document doc = new Document(@"D:\Temp\LetterHead\LetterHead.doc");

Console.WriteLine("---Before---");
for (int i=0; i < doc.Sections.Count; i++)
{
    Section sec = doc.Sections[i];
    foreach(HeaderFooter hf in sec.HeadersFooters)
    {
        Console.Write(hf.HeaderFooterType + " is : ");
        if (hf == null)
        {
            Console.WriteLine("NULL");
        }
        else
        {
            Console.WriteLine("NOT NULL");
        }

        if (string.IsNullOrEmpty(hf.ToString(SaveFormat.Text).Trim()))
        {
            hf.Remove();
        }
    }
}

Console.WriteLine();
Console.WriteLine("---After---");

for (int i = 0; i < doc.Sections.Count; i++)
{
    Section sec = doc.Sections[i];

    Console.WriteLine("HeaderEven " + ((sec.HeadersFooters[HeaderFooterType.HeaderEven] == null) ? "is : NULL" : " is : NOT NULL"));
    Console.WriteLine("HeaderFirst " + ((sec.HeadersFooters[HeaderFooterType.HeaderFirst] == null) ? "is : NULL" : " is : NOT NULL"));
    Console.WriteLine("HeaderPrimary " + ((sec.HeadersFooters[HeaderFooterType.HeaderPrimary] == null) ? "is : NULL" : " is : NOT NULL"));
    Console.WriteLine("FooterEven " + ((sec.HeadersFooters[HeaderFooterType.FooterEven] == null) ? "is : NULL" : " is : NOT NULL"));
    Console.WriteLine("FooterFirst " + ((sec.HeadersFooters[HeaderFooterType.FooterFirst] == null) ? "is : NULL" : " is : NOT NULL"));
    Console.WriteLine("FooterPrimary " + ((sec.HeadersFooters[HeaderFooterType.FooterPrimary] == null) ? "is : NULL" : " is : NOT NULL"));
}

doc.Save(@"D:\Temp\LetterHead\18.5.doc");