Header/foot images removed due to section removal

Hi there,

I was searching in the forums for some sample code on searching for a bookmark and removing the associated section and I found this piece...

DocumentBuilder builder = new DocumentBuilder(doc);

Node node = builder.Document.Range.Bookmarks[bmark].BookmarkStart;

while (node.NodeType != NodeType.Section)

{

node = node.ParentNode;

}

Aspose.Words.Section sectionToRemove = (Aspose.Words.Section) node;

sectionToRemove.Remove();

This code works great, it removes the sections I want it to...however, I've been experiencing some voodoo events going on. If I remove certain sections in my document, the images in my header and footer disappear (for all pages). For example, I have a document with a cover page, table of contents and some other pages. If I remove the table of contents, all the header and footer images get removed as well. If I remove some other page down the road, everything holds together and looks great. Any insight as to whats going on? maybe my document is structured incorrectly...I duno...

John

The code for bookmarked section removal can be made easier:

Bookmark bookmark = doc.Range.Bookmarks["bookmark1"];

if (bookmark != null)

{

bookmark.BookmarkStart.GetAncestor(typeof(Aspose.Words.Section)).Remove();

}

Concerning headers/footers, please mind that headers/footers belong to a particular section and subsequent sections can take headers/footers from the previous sections. It can be controlled by Section.HeadersFoooters.LinkToPrevious method. In your case the headers/footers are most probably defined in the section with table of contents. And other sections are linked to these headers/footers. So when you remove TOC section, every other section looses its headers/footers too.

Thanks for the reply.

I've tried the suggestion you mentioned and it is true. The table of contents was the "source" header/footer and all the other pages are linked. If I delete the table of contents, how would I make the next page contain the "source" header/footer?

Try using the following method for section removal:

private void RemoveSection(Section section, bool keepLinkedHeadersFooters)

{

if (keepLinkedHeadersFooters)

{

if (section != section.Document.LastSection)

{

Section nextSection = (Section)section.NextSibling;

foreach (HeaderFooter hf in section.HeadersFooters)

{

if (nextSection.HeadersFooters[hf.HeaderFooterType] == null)

nextSection.HeadersFooters.Add(section.HeadersFooters[hf.HeaderFooterType].Clone(true));

}

}

}

section.Remove();

}