How to modify displayed value of NumPagesField

7 posts, 1 answers
  1. ①Dr Mostafa
    ①Dr Mostafa avatar
    14 posts
    Member since:
    Oct 2015

    Posted 13 Aug Link to this post

    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
  2. ①Dr Mostafa
    ①Dr Mostafa avatar
    14 posts
    Member since:
    Oct 2015

    Posted 15 Aug Link to this post

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

    Many thanks....

  3. Mihail
    Admin
    Mihail avatar
    223 posts

    Posted 16 Aug Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
  4. ①Dr Mostafa
    ①Dr Mostafa avatar
    14 posts
    Member since:
    Oct 2015

    Posted 16 Aug in reply to Mihail Link to this post

    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 .....

  5. ①Dr Mostafa
    ①Dr Mostafa avatar
    14 posts
    Member since:
    Oct 2015

    Posted 19 Aug in reply to ①Dr Mostafa Link to this post

    Please some body help me ....

     

  6. Answer
    Mihail
    Admin
    Mihail avatar
    223 posts

    Posted 21 Aug Link to this post

    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.
  7. ①Dr Mostafa
    ①Dr Mostafa avatar
    14 posts
    Member since:
    Oct 2015

    Posted 21 Aug in reply to Mihail Link to this post

    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 ....

Back to Top