We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Header and Footer coordinates

I’m trying to get the Y coordinate (in points or pixels) of the bottom border of the header and the top border of the footer and PageSetup PageHeight, HeaderDistance, TopMargin, FooterDistance and BottomMargin don’t seem to get me the correct numbers.

what is the best way to achieve what I need.

Thanks

Hi Akram,


Thanks for your inquiry. There can be many sections in a document and PageSetup information may vary from section to section. Please attach your input document here for testing. We will investigate the issue on our end and provide you more information.

Edit: Are you trying to obtain coordinates (x, y) using LayoutCollector and LayoutEnumerator classes? If yes, please note that the LayoutCollector.GetEntity method works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape. It doesn’t work for Run, CellRow or Table nodes, and nodes within header/footer. The problem with headers/footers is that a single document node in header/footer has multiple corresponding objects in layout model – one per each page where this header/footer is rendered. So technically it’s not possible to return single entity for a node which is inside header/footer.

Best regards,

Hello,

I’m not trying to find coordinates of any node inside header and footer. I’m trying to get the y coordinate of the rendered document of the line that seperate the header to the body and the footer to the body.

In the docx file I’m attaching (which has different header footer sizes on first and other pages), if you edit the first page header per instance in ms word, you will see a dashed line that represent the line I’m looking for, it also have the label “First Page Header” right below it.

(please check the png file I’m attaching as well)


Hi Akram,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hello,
Any luck on this?

Hi Akram,

Thanks for being patient. We are checking with this scenario and will reply you within next 24 hours.

Best regards,

Please try executing the following code:

Document doc = new Document("D:\\temp\\HeaderFooter+Coordinates.docx");

LayoutEnumerator enumerator = new LayoutEnumerator(doc);

for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
{
    MarkHeaderFooterBoundaries(enumerator);
    enumerator.MoveNext();
}

doc.Save("D:\\temp\\18.9.docx");

Utility methods:

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

        if (enumerator.Type == LayoutEntityType.HeaderFooter)
        {
            AddLine(enumerator);
        }

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

    } while (enumerator.MovePrevious());
}

public static void AddLine(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 = enumerator.Rectangle.Top;
    }
    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;
    }

    Shape line = new Shape(enumerator.Document, ShapeType.Line);
    line.StrokeColor = Color.Red;
    line.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    line.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    line.WrapType = WrapType.None;

    builder.InsertNode(line);

    line.Width = enumerator.Rectangle.Width;
    line.Left = 0;
    line.Top = top;
}

Works great. Thanks