Free Support Forum - aspose.com

How to get the Height of Header and Footer using .NET | LayoutCollector.GetEntity

Hi,
I know, that LayoutCollector.GetEntity works only on paragraphs (please update the online documentation!). But if the paragraph is in a header/footer, it provides null too. Is that a bug or a feature?

@david.csillik.messerli

The LayoutCollector.GetEntity method works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape.

Please note that this method does not work for Run, CellRow or Table nodes, and nodes within header/footer.

Hi Tahir,
the first part is the opposite of the second: I have tried with a paragraph - it should work - within a header/footer - it should not work. Please extend the documentation with the constraints!
Than how can I calculate the height of the header/footer? I wanted to reach it just like you suggested for the tables: take a paragraph within, set as Current for LayoutEnumerator and navigate to the right node with the MoveParent() function, but here does not work.

@david.csillik.messerli

Following code example shows how to get the position of header where it ends and position of footer where it is started. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

MarkHeaderFooterBoundaries(enumerator);
public static void MarkHeaderFooterBoundaries(LayoutEnumerator enumerator)
{
    do
    {
        if (enumerator.MoveLastChild())
        {
            MarkHeaderFooterBoundaries(enumerator);
            enumerator.MoveParent();
        }

        if (enumerator.Type == LayoutEntityType.HeaderFooter)
        {
            double height = GetHeight(enumerator);
            Console.WriteLine(height);
        }

        // Stop after all elements on the page have been procesed.
        if (enumerator.Type == LayoutEntityType.Page)
            return;

    } while (enumerator.MovePrevious());
}
public static double GetHeight(LayoutEnumerator enumerator)
{
    double top = 0;
    DocumentBuilder builder = new
    DocumentBuilder(enumerator.Document);

    if (enumerator.Kind.Equals("FIRSTPAGEFOOTER"))
    {
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
        top = enumerator.Rectangle.Top;
    }
    else if (enumerator.Kind.Equals("PRIMARYFOOTER"))
    {
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
        top = builder.CurrentSection.PageSetup.PageHeight - enumerator.Rectangle.Top
            - builder.CurrentSection.PageSetup.HeaderDistance;
    }
    else if (enumerator.Kind.Equals("EVENPAGESFOOTER"))
    {
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
        top = enumerator.Rectangle.Top;
    }
    else if (enumerator.Kind.Equals("FIRSTPAGEHEADER"))
    {
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
        top = enumerator.Rectangle.Top + enumerator.Rectangle.Height;
    }
    else if (enumerator.Kind.Equals("PRIMARYHEADER"))
    {
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
        top = enumerator.Rectangle.Top + enumerator.Rectangle.Height;
    }
    else if (enumerator.Kind.Equals("EVENPAGESHEADER"))
    {
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
        top = enumerator.Rectangle.Top + enumerator.Rectangle.Height;
    }

    return top;
}

If you still face problem, please ZIP and attach your input Word document along with expected output. We will then provide you more information about your query.

1 Like

Hi Tahir,
apart from some mistakes, it works, thanks! :slight_smile:

@david.csillik.messerli

If this does not help you, you can share your issues detail.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.