How to get position of last element in RadDocument in pixels?

10 posts, 1 answers
  1. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 16 Apr Link to this post

    Hi I need to scale an image so that it fills all the empty space in a page. For this I am trying to find the remaining height of the page after some elements are inserted in it. I am using the following code:

    var position = new DocumentPosition(section.GetRootDocument());
    position.MoveToDocumentEnd();
    var pageContentHeight = section.PageSize.Height - position.Location.Y - section.PageMargin.Vertical;

     

    The problem is that position.Location.Y does't return a value in pixels. For example when my page is filed with text it returns 173.8107 but the height of the page is 1056. 

    How to get position of last element in RadDocument in pixels?

  2. Martin
    Admin
    Martin avatar
    90 posts

    Posted 20 Apr Link to this post

    Hello Remus,

    Instead of using this position location, I would suggest you to get the lastParagraph.LastLayoutBox.BoundingRectangle.Bottom value in order to calculate the remaining empty space on the page. Check the following code snippet: 

    Paragraph lastParagraph = this.radRichTextBox.Document.EnumerateChildrenOfType<Paragraph>().Last();
    float lastParagraphBoundingRectangleBottom = lastParagraph.LastLayoutBox.BoundingRectangle.Bottom;
    double pageContentHeight = section.PageSize.Height - lastParagraphBoundingRectangleBottom - section.PageMargin.Bottom;

    The lastParagraphBoundingRectangleBottom value includes the Section`s PageMargin.Top value, so you need to subtract only the PageMarging.Bottom.

    I hope this helps. Please, let me know if there is anything else I can assist you with.

    Regards,
    Martin
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 21 Apr in reply to Martin Link to this post

    Thank you for the response, but for some reason the bounding box of my paragraphs has all it's values set to 0. Do you have any idea why that might be?
  4. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 22 Apr in reply to Remus Link to this post

    I think that all the bounding rectangles are set to 0 because the RadDocument is not yet shown in the editor and it's in an unmeasured state.

    To give a bit more context to what I am trying to do, I am generating a read only document based on a macro and that means that I can't have the RadDocument be in a measured state when I try to calculate the remaining height of a page. Is there any other method I could get the remaining height?  

  5. Martin
    Admin
    Martin avatar
    90 posts

    Posted 23 Apr Link to this post

    Hi Remus,

    I possible solution to get the bounding rectangle size is to measure the RadDocument in an early stage (before it is passed to the editor). You can use the RadDocument`s EnsureDocumentMeasuredAndArranged() method. Check the following code snippet:

    document.EnsureDocumentMeasuredAndArranged();
    
    Paragraph lastParagraph = document.EnumerateChildrenOfType<Paragraph>().Last();
    Section section = document.EnumerateChildrenOfType<Section>().Last();
    float lastParagraphBoundingRectangleBottom = lastParagraph.LastLayoutBox.BoundingRectangle.Bottom;
    double pageContentHeight = section.PageSize.Height - lastParagraphBoundingRectangleBottom - section.PageMargin.Bottom;
    
    // TODO
    
    this.radRichTextBox.Document = document;

    I hope this is a solution for your case. Do not hesitate to contact us if any additional questions arise.

    Regards,
    Martin
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  6. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 24 Apr in reply to Martin Link to this post

    Hi Martin,

    Calling the EnsureDocumentMeasuredAndArranged() function did make the Bounding Rectangles to be measured, but there is still a problem: no matter how much text there is in a paragraph it's bounding rectangle has the same height, it only changes if I change the font family or size. The width dose change with the text thou.

    Also the measurements on the Y axis don't reset to 0 when the text enters a new page. 

  7. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 24 Apr Link to this post

    So if instead of EnsureDocumentMeasuredAndArranged() I use Measure() and Arrange() it's better, but I don't know what values are the best for the function parameters.

    I tried using

    section.GetRootDocument().Measure(new SizeF((float)section.PageSize.Width, (float)section.PageSize.Height));
    section.GetRootDocument().Arrange(new RectangleF(0, 0, (float)section.PageSize.Width, (float)section.PageSize.Height));

    but now the height is to large, can you explain a bit more what is the difference between the arrange and measure methods?

  8. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 27 Apr Link to this post

    So I discovered that my radDocument before being added in to the radRichTextBox has the Layout Mode set as Flow, not Paged. After setting it's layout as Paged, EnsureDocumentMeasuredAndArranged() woks better (the width is limited to page width minus page horizontal margins, and the left and top margins are added to the X and Y values), but the value of the BoundingRectangle.Bottom is still to large and the values on the Y axis don't reset to 0 when the paragraph goes on a new page.

    I have a few questions:

    1. Why are the values on the Y axis larger than the page height when the text that I have doesn't even fill a whole page?
    2. How can I calculate the remaining height of a page that is not the first page of the document? (I was thinking of subtracting the page height from BoundingRectangle.Bottom until it is lesser than the page height, but I don't know how to account for the margins.)
    3. What do Measure() and Arrange() do exactly?
  9. Answer
    Martin
    Admin
    Martin avatar
    90 posts

    Posted 28 Apr Link to this post

    Hi Remus,

    The EnsureDocumentMeasuredAndArranged() method calls recursively the Measure() and Arrange() methods to all document children respectively. The Measure() method is measuring the DocumentLayoutBox using RadDocument.MAX_DOCUMENT_SIZE and the Arrange() method is arranging the DocumentLayoutBox using new RectangleF(PointF.Empty, this.document.DesiredSize). Check the following code snippet:

    this.document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
    this.document.Arrange(new RectangleF(PointF.Empty, this.document.DesiredSize));

    You can download and take look at our source code if you want to get more in detail on a specific part of the code.

    I tried to change the LayoutMode to Paged or Flow and the result was the same. I am attaching a sample project demonstrating what I am testing. Please, feel free to modify it in a way closer to your scenario.

    As for the calculation of the remaining height of a Page, you can use the Modulus operator ("%") in order to get the remainder of the division. The margins are included when you use the BoundingRectangle.Botton and you don't need to take care of them. Check the following code snippet:

    double lastParagraphBoundingRectangleBottom = lastParagraph.LastLayoutBox.BoundingRectangle.Bottom % (double)section.PageSize.Height;
    

    As for the values on the Y-axis, in order to investigate it, I need more information on how exactly this behavior is achieved or a sample project demonstrating it. 

    Please, let me know if there is anything else I can assist you with.

    Regards,
    Martin
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  10. Remus
    Remus avatar
    15 posts
    Member since:
    Jan 2020

    Posted 29 Apr in reply to Martin Link to this post

    Hi Martin, thank you for your help!

    I have figured out why the values on the Y axis were larger than expected: I was applying a stylesheet to the document later in the code and the font size changed. After applying the stylesheet before calling EnsureDocumentMeasuredAndArranged() it works as expected.

Back to Top