Delete header and footer starting with section 2

Hi,
I’m editing an existing letter, it has header and footer. To this letter I have to attach further documents. No headers or footers should appear on these pages. Here is my code:

            DocumentBuilder builder = new DocumentBuilder(Document);
            builder.MoveToDocumentEnd();
            builder.InsertBreak(BreakType.SectionBreakNewPage);
            Section currentSection = builder.CurrentSection;
            currentSection.HeadersFooters.LinkToPrevious(false);
            if (currentSection != null) currentSection.HeadersFooters.Clear();

Unfortunately, the header and footer are still displayed as in the first section. How can I switch off the headers and footers from the second section onwards?

Thank you very much

@MarkusPlesoft

Could you please ZIP and attach your input and output word documents here for testing? We will investigate the issue and provide you more information on it.

okay, here are the files: HeaderFooter.zip (17.8 KB)

@MarkusPlesoft

Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + @"HeaderFooter_Template.dot");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
Section currentSection = builder.CurrentSection;
                
if (currentSection != null)
{ 
    currentSection.HeadersFooters.Clear();
    currentSection.HeadersFooters.LinkToPrevious(false);
}

doc.Save(MyDir + "output.docx");

Hi tahir.manzoor,

just swap two lines and it’s up and running. You have to think about it first :slight_smile: But it works!

@MarkusPlesoft

It is nice to hear from you that your problem has been solved.

HeadersFooters.Clear removes all nodes from this collection and from the document. When header and footer are cleared, they are linked to previous header and footer. So, you need to unlink them using HeadersFooters.LinkToPrevious method. Hope this answers your query.