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

Custom Annotation Issue

1 Answer 60 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 2
Patrick asked on 30 May 2012, 03:54 PM
Hi,

I programmed a custom annotation insertion and, I have an issue when custom annotation's prefix are exported. Here is the fragment of code to make the custom annotation:
Public Class NovoBookMarkRangeStart
    Inherits FieldRangeStartBase
  
    Private m_Name As String
    Private m_TypeCase As Integer
    Private m_BookFormat As String
  
    <XamlSerializable()> _
    Public Property BookFormat As String
        Get
            Return m_BookFormat
        End Get
        Set(value As String)
            m_BookFormat = value
        End Set
    End Property
  
    <XamlSerializable()> _
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(value As String)
            m_Name = value
        End Set
    End Property
  
    <XamlSerializable()> _
    Public Property TypeCase() As Integer
        Get
            Return m_TypeCase
        End Get
        Set(value As Integer)
            m_TypeCase = value
        End Set
    End Property
  
      
  
    Public Sub New()
    End Sub
  
    Public Sub New(name As String, TypeCase As Integer, Bookformat As String)
        Me.TypeCase = TypeCase
        Me.Name = name
        Me.BookFormat = Bookformat
    End Sub

Here is the code to put the annotation in the document :
Private Sub Wpf_NewBookMark(ID As Integer,  TypeCase As Integer, BookFormat As String) Handles Wpf.NewBookMark
         
       Dim StartRange As New NovoBookMarkRangeStart(ID.ToString, TypeCase, BookFormat)
       Dim EndRange As New NovoBookMarkRangeEnd()
       'Caret Position
       Dim S As New DocumentPosition(Me.editor.Document.CaretPosition)
       Dim E As New DocumentPosition(Me.editor.Document)
       EndRange.PairWithStart(StartRange)
       'Custom Span
       Dim BookMarkSpan As New Span("{" & Id.ToString() & "}")
       S.AnchorToCurrentBoxIndex()
       editor.InsertInline(StartRange)
       editor.InsertInline(BookMarkSpan)
       editor.InsertInline(EndRange)
       S.RestorePositionFromBoxIndex()
       editor.UpdateEditorLayout()
         
   End Sub

So, when I save the document under a .XAML format, the section with the custom annotation does not save the TypeCase value but put the prefix and, the BookFormat prefix is not there at all. On the other hand, the Name prefix is with the good value. Here is a sample fragment :

<t:Section PageMargin="96,96,96,96" PageSize="816,1056">
    <t:Paragraph LineSpacing="1" StyleName="Normal">
      <custom1:NovoBookMarkRangeStart AnnotationID="1" Name="5" TypeCase="0" />
      <t:Span Text="{}{Patient.PostalCode}" />
      <custom1:NovoBookMarkRangeEnd AnnotationID="1" />
    </t:Paragraph>
  </t:Section>

So, can you find why the BookFormat prefix and value are not present and, why the the TypeCase value is the wrong one?

Thank a lot,

Patrick

1 Answer, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 04 Jun 2012, 04:48 PM
Hi Patrick,

I tested your code with the latest official release, but there were several problems.

First of all, the NovoBookMarkRangeStart does not override two of the required methods: 
Protected Overrides Function CreateNewElementInstance() As DocumentElement
    ' TODO: Implement this method
    Return New NovoBookMarkRangeStart()
End Function
 
Protected Overrides Sub CopyContentFromOverride(fromElement As DocumentElement)
    ' TODO: Implement this method

End Sub

As you can see, the CopyContentFromOverride method can be left empty, but that is not the case with the CreateNewElementInstance.

When it comes to inserting the range start and range end, this can be done in one of the following methods:
1. Remember the start position, add other content, get the end position. Select the part of the document between the start and end position and use the InsertAnnotationRange method like this:
Private Sub Wpf_NewBookMark(ID As Integer, TypeCase As Integer, BookFormat As String)
    Dim StartRange As New NovoBookMarkRangeStart(ID.ToString, TypeCase, BookFormat)
    Dim EndRange As New NovoBookMarkRangeEnd()
    EndRange.PairWithStart(StartRange)
 
    Dim startPosition = New DocumentPosition(Me.editor.Document.CaretPosition)
    startPosition.AnchorToCurrentBoxIndex()
 
    'Custom Span
    Dim BookMarkSpan As New Span("{" & ID.ToString() & "}")
 
    editor.InsertInline(BookMarkSpan)
 
    startPosition.RestorePositionFromBoxIndex()
    editor.Document.Selection.SetSelectionStart(startPosition)
    editor.Document.Selection.AddSelectionEnd(Me.editor.Document.CaretPosition)
    editor.InsertAnnotationRange(StartRange, EndRange)
 
End Sub

2. This method can only be used if the range start and range end are copyable. In order to make them Copyable, you have to override the IsCopyable property in both the RangeStart and RangeEnd classes. In addition, in the class where you keep your custom properties, you have to add the logic of copying the values of the properties. Here is an example:
Public Overrides ReadOnly Property IsCopyable As Boolean
    Get
        Return True
    End Get
End Property
 
Protected Overrides Sub CopyPropertiesFromOverride(fromElement As Telerik.Windows.Documents.Model.DocumentElement)
    MyBase.CopyPropertiesFromOverride(fromElement)
    Dim fromRangeStart = DirectCast(fromElement, NovoBookMarkRangeStart)
    Me.Name = fromRangeStart.Name
    Me.TypeCase = fromRangeStart.TypeCase
    Me.BookFormat = fromRangeStart.BookFormat
End Sub

If you have implemented the copy logic in this way, you can insert the range start and range end in the following way:
Dim StartRange As New NovoBookMarkRangeStart(ID.ToString, TypeCase, BookFormat)
Dim EndRange As New NovoBookMarkRangeEnd()
EndRange.PairWithStart(StartRange)
 
Dim BookMarkSpan As New Span("{" & ID.ToString() & "}")
 
Dim document = New RadDocument()
Dim section = New Section()
Dim paragraph = New Paragraph()
paragraph.Inlines.Add(StartRange)
paragraph.Inlines.Add(BookMarkSpan)
paragraph.Inlines.Add(EndRange)
section.Blocks.Add(paragraph)
document.Sections.Add(section)
document.MeasureAndArrangeInDefaultSize()
document.Selection.SelectAll()
Dim fragment = document.Selection.CopySelectedDocumentElements()
editor.InsertFragment(fragment)

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

Kind regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Patrick
Top achievements
Rank 2
Answers by
Iva Toteva
Telerik team
Share this question
or