This is a migrated thread and some comments may be shown as answers.

Finding Page Numbers

4 Answers 151 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Sandra
Top achievements
Rank 1
Sandra asked on 12 Apr 2014, 03:03 AM
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;
        }




4 Answers, 1 is accepted

Sort by
0
Missing User
answered on 15 Apr 2014, 02:00 PM
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.

 
0
Sandra
Top achievements
Rank 1
answered on 15 Apr 2014, 02:04 PM
What if a Section spans multiple pages? Will this still work?
0
Sandra
Top achievements
Rank 1
answered on 15 Apr 2014, 05:45 PM
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.


0
Missing User
answered on 17 Apr 2014, 01:39 PM
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.

 
Tags
RichTextBox
Asked by
Sandra
Top achievements
Rank 1
Answers by
Missing User
Sandra
Top achievements
Rank 1
Share this question
or