Finding Page Numbers

5 posts, 0 answers
  1. Sandra
    Sandra avatar
    16 posts
    Member since:
    Sep 2011

    Posted 11 Apr 2014 Link to this post

    I have a RadDocument that I am building up programatically. Within the document I have five Sections and in one of the section elements I have added a Footer and inserted a PageField to dynamically generate the page number during rendering of the document.

    All of that is working and I am, so far, content. However...

    I need the page numbers from that section for use in another section (building an index with key terms and references). I found the following code somewhere on the web but it isn't working properly for me...

    For the first page when I make the call the ChildIndex is 3. I'm not sure if this is good or bad but it is most definitely not the solution since the first page of the document is only one page so I should be getting 1. I'm obviously doing *something* incorrectly but am not certain what that is.

    Any insight is greatly appreciated!

    rjsjr

                var box = mergedDocument.CaretPosition.GetCurrentInlineBox();

                if (box != null)
                {
                    DocumentElement element = box.AssociatedDocumentElement;
                    var page = GetPageNumberForDocumentElement(element);
                }

            private int GetPageNumberForDocumentElement(DocumentElement element)
            {
                var layoutBox = GetSectionLayoutBox(element);
                return layoutBox.ChildIndex + 1;
            }

            private LayoutBox GetSectionLayoutBox(DocumentElement element)
            {
                LayoutBox layoutBox = element.FirstLayoutBox;
                while (!(layoutBox is SectionLayoutBox))
                {
                    layoutBox = layoutBox.Parent;
                }

                return layoutBox;
            }




  2. Yancho
    Admin
    Yancho avatar
    72 posts

    Posted 15 Apr 2014 Link to this post

    Hello,

    If your desired scenario is to find the number of the page on current caret position, I would recommend you to use the GetCurrentSectionBox() method of document’s CarretPosition property. This way you would be able to take the current section PageNumber property. The following code-snippet illustrates how this could be accomplished:
    int pageNumber = document.CaretPosition.GetCurrentSectionBox().PageNumber;

    I hope this helps! Let me know if you have other comments or questions.

    Regards,
    Yancho
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Sandra
    Sandra avatar
    16 posts
    Member since:
    Sep 2011

    Posted 15 Apr 2014 in reply to Yancho Link to this post

    What if a Section spans multiple pages? Will this still work?
  5. Sandra
    Sandra avatar
    16 posts
    Member since:
    Sep 2011

    Posted 15 Apr 2014 in reply to Yancho Link to this post

    Almost... 

    To get the Caret in position I call document.CaretPosition.MoveToEndOfDocumentElement(sec); where "sec" is the Section that I want to be inserting in.

    On the first page I am getting the GetCurrentSectionBox().PageNumber is returning the correct page number (4) based on the number of preceding sections (3) and I can caluclate from there. That's good.

    After I get the page number I then insert my content (just using InsertFragment so it inserts at the CaretPosition) and then call InsertPageBreak. 

    When I call InsertPageBreak the PageNumber does not change. I then insert more content and the page number does change to 5 after that.

    That's unexpected to me.


  6. Yancho
    Admin
    Yancho avatar
    72 posts

    Posted 17 Apr 2014 Link to this post

    Hello Sandra,

    I‘m going to discuss your questions one by one.

    Telerik RadRichTextBox supports multiple sections. Sections are chunks of document that can be displayed on one or several pages. If you follow the given suggestion from my previous answer, you could find the number of the page on current caret position no matter if the section is one, two or more pages long.

    As to your second question, we recently changed the layout of the page break symbols, and now it is allowed paragraph or section break symbols to be laid out on the same page in some conditions. This behavior is consistent with MS Word and was demanded by many customers.

    What I can recommend you is to insert a newline symbol (this.radRichTextBox.Insert("\n");) after the page break symbol. This way you will ensure that the following symbol will be laid out on different page.

    I hope this helps! If you have any comments or other questions, do not hesitate to contact us again.

    Regards,
    Yancho
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WPF is Visual Studio 2017 Ready