We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to only clear headers or footers in document?

Hi, Support,

I have the question that how to only clear headers or footers in document? That’s to say:
I only want to clear header and keep footer unchanged or clear footer and keep header unchanged, how to do it?

        For Each section As Aspose.Words.Section In Doc.Sections
            section.HeadersFooters.Clear()
        Next

By using the above code, all the header and footer are cleared, but I only want to clear the headers.

Thanks for your help.

Ducaisoft

@ducaisoft,

Please try using the following code:

SectionCollection sections = doc.Sections;
foreach (Section section in sections)
{
    foreach (HeaderFooter header in section.HeadersFooters)
    {
        if (header.HeaderFooterType == HeaderFooterType.HeaderPrimary ||
            header.HeaderFooterType == HeaderFooterType.HeaderFirst ||
            header.HeaderFooterType == HeaderFooterType.HeaderEven)
        {
            header.Remove();
        }
    }
}