Detect whether a shape or table is part of header or footer or body

Hello,

How to detect whether a shape or table is part of header or footer or body?
I need to do some operations on shapes and tables but those should be inside the body only for whole document.

Thanks,
Varun

@varun.arora

You can use Node.GetAncestor method (NodeType) as shown below to achieve your requirement. Please do the same for Shape node. We suggest you please about Aspose.Words’ DOM here:

Document doc = new Document(MyDir + "in.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
if (table.GetAncestor(NodeType.Body) != null)
{
    Console.WriteLine("Table is child node of Body node");
}
else if (table.GetAncestor(NodeType.HeaderFooter) != null)
{
    Console.WriteLine("Table is child node of Header/Footer node");
}

Thank you. That works

How to know if node is in Header only or footer only…?

@Jaibir you can use HeaderFooter.HeaderFooterType to check type of the header footer node.

1 Like

Can you please update the following code to find out if node is in header only or footer only.
Thank you!

Document doc = new Document(MyDir + "in.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
if (table.GetAncestor(NodeType.Body) != null)
{
    Console.WriteLine("Table is child node of Body node");
}
else if (table.GetAncestor(NodeType.HeaderFooter) != null)
{
    Console.WriteLine("Table is child node of Header/Footer node");
}

@Jaibir Please see the following modified code:

Document doc = new Document(MyDir + "in.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
if (table.GetAncestor(NodeType.Body) != null)
{
    Console.WriteLine("Table is child node of Body node");
}
else if (table.GetAncestor(NodeType.HeaderFooter) != null)
{
    HeaderFooter hf = (HeaderFooter)table.GetAncestor(NodeType.HeaderFooter);
    Console.WriteLine("Table is child node of Header/Footer node " + hf.HeaderFooterType);
}
1 Like