Search code examples
c#migradoc

MigraDoc - Get height of section


What I'm trying to achieve is to have the same content twice on the page, separated by a dotted line, with padding so that the one copy is on the top-half and the second is on the bottom half. Something like firstSection.SpaceAfter = (height of page / 2) - (height of first section)

tl;dr: How do I calculate the height of the first section after everything is added?

I have this, which has the copied content and the dotted line, but I need the padding.

Document document = new Document();
document.Info.Title = "Testing";

Style style = document.Styles["Normal"];
style.Font = new Font("Times New Roman", 12);

Section section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();

Paragraph paragraph = section.AddParagraph("Hello");
paragraph = section.AddParagraph("World");
paragraph = section.AddParagraph();
paragraph.Format.Borders.Bottom = new Border
{
    Width = "1pt",
    Color = Colors.Black,
    Style = BorderStyle.DashLargeGap
};

paragraph = section.AddParagraph("Hello");
paragraph = section.AddParagraph("World");

Solution

  • To answer your question:
    The content only has a height when you render it to some output format (e.g. PDF).
    Let the PDF Renderer prepare the document and then you will be able to query position and height for every object in the document.

    Alternative solutions:
    My attempt #1 would be: create a Table and set the height of the first row to cover the top half of the page; add the contents for the lower half of the page to the second row; hide the borders to make the table invisible.
    My attempt #2 would be: create a TextFrame at an absolute position for the lower half of the page; add the contents normally to the section and also add them to the TextFrame.

    In both cases you must make sure the contents fit into a half page.