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

How to modify displayed value of NumPagesField

8 Answers 60 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
①Dr Mostafa
Top achievements
Rank 1
①Dr Mostafa asked on 13 Aug 2017, 10:03 PM

Hi every body ....

I use RadRichTextBox in the following manner: first page is used (by the user) as a cover letter so that it is not included within the total count of pages. Footer of every page except first page should display page number x from y, where x is the current page number (after the first page), and y is the total number of pages minus one (the first page).

I could achieve the first value of the formatted footer using CodeBasedField, but unfortunately I couldn't modify the value returned by NumPagesField.

My question is how to modify value returned by NumPagesField so that I can display total number of pages minus one. Below there is an attachment of an image to show my requirement.

The following is my code snippet used in creating the custom footer:

Public Function FirstTemplate() As RadDocument
Dim section As New Section()
 section.HasDifferentFirstPageHeaderFooter = True
section.Footers.[Default] = New Footer With {.Body = ReportFooter()}
End Function
 
Private Function ReportFooter() As RadDocument
    Dim footerDoc As New RadDocument
Dim footerSection As New Section
            Dim footerParagraph As New Paragraph
            Dim footerDocumentEditor As New RadDocumentEditor(footerDoc)
 
            footerParagraph.TextAlignment = RadTextAlignment.Center
            footerDoc.Sections.Add(footerSection)
            footerSection.Blocks.Add(footerParagraph)
            footerDocumentEditor.InsertField(New FormattedPageNumber, FieldDisplayMode.Result)
            footerDocumentEditor.InsertField(New NumPagesField, FieldDisplayMode.Result) '??? how to return total number of pages -1
 
            Return footerDoc
        End Function
 
Public Class FormattedPageNumber
        Inherits CodeBasedField
        Public Shared ReadOnly FieldType As String = "FormattedPageNUM"
 
        Shared Sub New()
            CodeBasedFieldFactory.RegisterFieldType(FormattedPageNumber.FieldType, Function()
                                                                                       Return New FormattedPageNumber()
                                                                                   End Function)
        End Sub
 
        Public Overrides ReadOnly Property FieldTypeName() As String
            Get
                Return FormattedPageNumber.FieldType
            End Get
        End Property
 
        Protected Overrides Function GetResultFragment() As DocumentFragment
            Dim pageNumber As Integer = 0
            'Dim pagesCount As Integer = 0
            If Me.Document IsNot Nothing Then
                Dim position As New DocumentPosition(Me.Document)
                position.MoveToStartOfDocumentElement(Me.FieldStart)
                Dim sectionBox As SectionLayoutBox = position.GetCurrentSectionBox()
 
                pageNumber = sectionBox.PageNumber
            End If
 
            Dim resultString As String = "Page " & pageNumber - 1 & " From "
 
            Return MyBase.CreateFragmentFromText(resultString)
        End Function
 
        Public Overrides Function CreateInstance() As Field
            Return New FormattedPageNumber()
        End Function
    End Class

8 Answers, 1 is accepted

Sort by
0
①Dr Mostafa
Top achievements
Rank 1
answered on 15 Aug 2017, 08:13 PM

Please the issue is urgent ... any help will be greatly appreciated.

Many thanks....

0
Mihail
Telerik team
answered on 16 Aug 2017, 08:44 AM
Hello Mostafa,

You have the correct approach in calculating which is the first page with a CodeBasedField. You could do the same for the num pages as well. What you need to do is to calculate the total number of pages and subtract the initial pages, in your case just the first one.

I will recommend you to check how the NumPagesField is implemented and mirror it with the difference of the total number of pages calculation.
In addition, I will mention that the pages are actually the section layout boxes and each section layout box has a property named PageNumber. If you obtain the last section box in a document, you will get the total number of pages.

I hope this information answers your questions.

Regards,
Mihail
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
①Dr Mostafa
Top achievements
Rank 1
answered on 16 Aug 2017, 05:37 PM

Thank you very much Mihail for your quick response.

I already tried using CodeBasedField to get total number of pages - 1, but I always return 0. the code sippet that I used is:

Protected Overrides Function GetResultFragment() As DocumentFragment
            Dim pageNumber As Integer = 0
            Dim pagesCount As Integer = 0
 
            If Me.Document IsNot Nothing Then
                Dim position As New DocumentPosition(Me.Document)
                position.MoveToStartOfDocumentElement(Me.FieldStart)
                Dim sectionBox As SectionLayoutBox = position.GetCurrentSectionBox()
                 
                pageNumber = sectionBox.PageNumber
                pagesCount = DocumentStructureCollection.GetChildrenCount(Me.Document.DocumentLayoutBox)
            End If
 
            Dim resultString As String = "Page " & pageNumber - 1 & " From " & pagesCount -1
 
            Return MyBase.CreateFragmentFromText(resultString)
        End Function

As I could understand, this behavior was the result of implementation being used within the footer, and hence telerik team added NumPagesField, so that it could be used within the footer.

I searched the web for NumPagesField implementation, as you suggest, but I didn't find any results, in addition I'm new to telerik RadRichTextBox control. So could you please show me how to mirror NumPagesField implementation, or how to get the last section layout box of the document, to achieve my requirement.

Thank you very much .....

0
①Dr Mostafa
Top achievements
Rank 1
answered on 19 Aug 2017, 09:11 PM

Please some body help me ....

 

0
Accepted
Mihail
Telerik team
answered on 21 Aug 2017, 08:07 AM
Hello Mostafa,

It seems that a field inserted in a header or footer is not able to access the properties of the main document meaning that it is not able to get the total number of pages. I have logged a new feature request for this. Here is the link: Allow field inserted in child document (header/footer) to access properties from the main document.

I am afraid that there is no workaround for this particular scenario. However, if the field is inserted in the document itself it will be updated as required.

Regards,
Mihail
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
①Dr Mostafa
Top achievements
Rank 1
answered on 21 Aug 2017, 08:46 PM

This is disappointing for me, any way thank you very much Mihail for your response, and please if you find a workaround for this particular requirement, then i will be very happy if you inform me.

Thank you ....

0
Stefania
Top achievements
Rank 2
answered on 21 Dec 2018, 03:18 PM

Hi, I have the same problem.

Is it fixed or did you find a workaround?

 

Thank you

0
Peshito
Telerik team
answered on 26 Dec 2018, 07:08 AM
Hello Stefania,

This feature request is not yet implemented and there isn't a workaround for this scenario. The best way to get notified when its status changed is to subscribe for the public item -  Allow field inserted in child document (header/footer) to access properties from the main document.

Regards,
Peshito
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextBox
Asked by
①Dr Mostafa
Top achievements
Rank 1
Answers by
①Dr Mostafa
Top achievements
Rank 1
Mihail
Telerik team
Stefania
Top achievements
Rank 2
Peshito
Telerik team
Share this question
or