Get a documents header 'height'

Hi,

I’ve been trying a few different methods for figuring out the ‘height’ of a header in a document, that is to say how much space the header is taking up before the actual body of the document is shown.

So far I have tried to achieve this by inspecting the CurrentSection.PageSetup.TopMargin and CurrentSection.PageSetup.HeaderDistance values of the document builder but these don’t ever seem to change. If I had a header that was only a single line of text as opposed to one that has 4 or 5 lines of text in it, I don’t seem to be seeing any change in those 2 values above.

I need to somehow be able to know just how big the header section is as I need to generate a textbox that matches this size before inserting it into the header to cover the existing content, this bit is all working fine, it is just getting the correct height that seems to be the problem.

Any advice?

Many thanks

Furthermore, I imagine whichever method ends up being used, I will be able to change ‘header’ to ‘footer’ or ‘top’ to ‘bottom’ to be able to get the height measurement for the footer as well?

Hi Jak,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

Please attach your input Word documents.
Please
attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document.

We will investigate how you are expecting your final document be generated like. We will then provide you more information on this along with code.

Hi Tahir,

Thanks for that, I’ve attached the source and target documents, hopefully they’l demonstrate what I’m aiming for a little better.

Hi Jak,

Thanks for sharing the detail. Please use the following code example to achieve your requirements. How this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Source.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
double distance = doc.FirstSection.PageSetup.TopMargin + doc.FirstSection.PageSetup.HeaderDistance;
Shape textBox = new Shape(builder.Document, ShapeType.TextBox);
textBox.Height = distance;
textBox.Width = doc.FirstSection.PageSetup.PageWidth;
textBox.WrapType = WrapType.Square;
textBox.Left = 0;
textBox.Top = -doc.FirstSection.PageSetup.HeaderDistance;
textBox.AppendChild(new Paragraph(doc));
Paragraph para = textBox.FirstParagraph;
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// Add some text to the paragraph.
Run run = new Run(doc);
run.Text = "Textbox is floated above the text so the original headers and footers are preserved. Can get the overall page width fine, but the height is the issue.";
para.AppendChild(run);
doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].FirstParagraph.AppendChild(textBox);
doc.Save(MyDir + "Out.docx");

Hi Tahir,

Thanks for that code, I’ve implemented it into my test code and unfortunately the result is still not quite right. Also in the snippet of code you provided, the ‘double headerdistance’ doesn’t actually seem to be used anywhere, I assume that it can just be used as the textbox.Top value and achieve the same result as -doc.FirstSection.PageSetup.HeaderDistance

I’ve attached my updated test document and I have also copied in my current test code below to hopefully help recreate the issue I’m experiencing or to help you show where I am going wrong. You’ll see that on the first 2 pages, the header doesn’t extend far enough and on the third and forth pages it extends too far.

int sectionIndex++;

foreach (Section section in _document.Sections)
{
    var width = section.PageSetup.PageWidth;
    var headerHeight = section.PageSetup.TopMargin + section.PageSetup.HeaderDistance;

    builder.MoveToSection(sectionIndex);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

    Shape headerbox = new Shape(_document, ShapeType.TextBox);
    headerbox.Name = "TestBox";
    headerbox.Width = width;
    headerbox.Height = headerHeight;
    headerbox.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    headerbox.VerticalAlignment = VerticalAlignment.Top;
    headerbox.HorizontalAlignment = HorizontalAlignment.Center;
    headerbox.WrapType = WrapType.None;
    headerbox.BehindText = false;
    headerbox.FillColor = System.Drawing.Color.White;
    headerbox.Filled = true;
    headerbox.Stroked = false;
    headerbox.Top = -section.PageSetup.HeaderDistance;

    Paragraph headerParagraph = new Paragraph(_document);
    headerParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Centre;

    headerbox.AppendChild(headerParagraph);

    Run headerText = new Run(_document, TEST CONTENT);
    headerText.Font.Size = 20;
    headerText.Font.Color = System.Drawing.Color.Red;
    headerText.Font.Name = "Arial";

    headerParagraph.AppendChild(headerText);

    builder.InsertNode(headerbox);
    sectionIndex++;
}

Thanks for your help on this.

Hi Jak,

Thanks
for your inquiry. There is one extra line of code in my previous post and I have removed that line. In your case, the PageSetup.TopMargin return correct value. However, in your document, the header’s content flow down to the page which increase the header’s height more then the value of TopMargin.

In this case, I suggest you please get the position of first paragraph of section’s body using LayoutEnumerator.Rectangle. This property returns the bounding rectangle of the current entity relative to the page top left corner (in points). Please use the following code example to achieve your requirements.

Hope this helps you. Please let us know if you have any more queries.

Document _document = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(_document);
int sectionIndex;
sectionIndex = 0;
LayoutCollector layoutCollector = new LayoutCollector(_document);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(_document);
foreach (Section section in _document.Sections)
{
    var renderObject = layoutCollector.GetEntity(section.Body.FirstParagraph);
    layoutEnumerator.Current = renderObject;
    RectangleF location = layoutEnumerator.Rectangle;
    var width = section.PageSetup.PageWidth;
    var headerHeight = location.Y;
    builder.MoveToSection(sectionIndex);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    Shape headerbox = new Shape(_document, ShapeType.TextBox);
    headerbox.Name = "TestBox";
    headerbox.Width = width;
    headerbox.Height = headerHeight;
    headerbox.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    headerbox.VerticalAlignment = VerticalAlignment.Top;
    headerbox.HorizontalAlignment = HorizontalAlignment.Center;
    headerbox.WrapType = WrapType.None;
    headerbox.BehindText = false;
    headerbox.FillColor = System.Drawing.Color.White;
    headerbox.Filled = true;
    headerbox.Stroked = false;
    headerbox.Top = -section.PageSetup.HeaderDistance;
    Paragraph headerParagraph = new Paragraph(_document);
    headerParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    headerbox.AppendChild(headerParagraph);
    Run headerText = new Run(_document, "TEST CONTENT");
    headerText.Font.Size = 20;
    headerText.Font.Color = System.Drawing.Color.Red;
    headerText.Font.Name = "Arial";
    headerParagraph.AppendChild(headerText);
    builder.InsertNode(headerbox);
    sectionIndex++;
}
_document.Save(MyDir + "Out.docx");

Hi Tahir,

Thanks for that, that has worked perfectly, is getting a much more accurate height as well compared to the old method.

One final question, how would this logic be carried over to the footer, since it’s using the location from the top left of the page will I need to inspect the last section of the page and see where the boundary is?

Thanks

Hi Jak,

Thanks
for your inquiry. We are working over your query and will update you asap.

Hi Jak,

Thanks
for your patience. To get the position of first paragraph of footer, you need to Aspose.Words.Layout API. Unfortunately, LayoutCollector.GetEntity return null value for header/footer items. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10820. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

A post was split to a new topic: Get height of footer