How do i know if cursor is in main document or in a header/footer?

Hello,

In order to do a search operation using replace, i need to know if the cursor is located inside the document’s body, header or footer.
Indeed i don’t want to search inside the document’s body if the cursor is inside the primary header for instance.

How can i get this information ?

@guyyyyyyyyyyy Do you mean DocumentBuilder cursor? If so, you can simply check whether the current node has Header/Footer as an ancestor:

bool isInHeaderFooter = builder.CurrentNode.GetAncestor(NodeType.HeaderFooter) != null;

Indeed i mean the DocumentBuilder cursor, is there a way to differenciate the header first, header primary, footer first and footer primary ?

@guyyyyyyyyyyy Sure, you can use code like the following to achieve this:

HeaderFooter headerFooter = (HeaderFooter)builder.CurrentNode.GetAncestor(NodeType.HeaderFooter);
bool isInHeaderFooter = headerFooter != null;
if(isInHeaderFooter) 
    Console.WriteLine(headerFooter.HeaderFooterType);
1 Like