Document processing (Rich textbox) unexpected behavior when html anchor tag with not text is imported by using RtfFormatProvider

1 Answer 27 Views
RichTextBox
Kamran
Top achievements
Rank 3
Iron
Iron
Veteran
Kamran asked on 06 May 2022, 04:43 PM

Anchor tag with not text is by default not shown in HTML document. Such anchor tags are used for internal purpose.

<a href="/EORWeb/Download?/6971042495152-I.D card.pdf" target="_blank"/>

Importing Html document in Rad rich text box is working as expected, but if same document is converted to RTF first by using RtfFormatProvider or save html imported document as rtf; you will notice such anchor tags are visible in Richtextbox editor.

This behavior make consumer annoying, as he/she is expecting same behavior in standard html document. I checked import/export settings for both RtfFormatProvider and HtmlFormateProvider to stop such anchor tags.

For reference I am attaching sample html document, which contains such anchor tags, but supposed not to be appear in editor. and following RadRichtextbox behvior.

 

Correct behavior when importing html document

 

RFT document behavior. (Highlighted anchors need to be hidden)

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 11 May 2022, 09:51 AM

Hello Kamran,

The behavior you are observing is related to the specifics of the RTF format. It doesn't allow having an empty field result. Even when the property is left empty (as RadRichTextBox does), the result is recalculated and added to the field. I checked the behavior of MS Words as well - for this particular document, the hyperlink is not imported, thus it is not present in the RTF document after saving it. If you create a DOCX document containing a hyperlink without text to display and import this document into Word, then export it to RTF, you will notice that the behavior is the same. Investigating the generated RTF content from both, RadRichTextBox and MS Word, shows that the \fldrslt RTF tag that usually preserves the display text of a hyperlink is empty. Therefore, we are not considering this as a bug in the control - it is related to document format specifics.

There are two approaches you can take to overcome that limitation:

  1. Delete the hyperlinks that do not have any text
  2. Add a space to ensure each hyperlink has text

Here is how you can achieve both:

IEnumerable<HyperlinkRangeStart> hyperlinks = this.radRichTextBox.Document.EnumerateChildrenOfType<HyperlinkRangeStart>().ToList();
foreach (var hyperlinkStart in hyperlinks)
{
    DocumentPosition position = new DocumentPosition(this.radRichTextBox.Document);
    position.MoveToInline(hyperlinkStart);
    position.MoveToNext();

    HyperlinkRangeEnd hyperlinkEnd = position.GetCurrentInline() as HyperlinkRangeEnd;
    if (hyperlinkEnd != null)
    {
        // There is no content between the start and end ranges of the hyperlink.
        // Approach 1: Remove the link
        // this.radRichTextBox.DeleteAnnotationRange(hyperlinkStart);

        // Approach 2: Insert a space as hyperlink content
        this.radRichTextBox.Document.CaretPosition.MoveToPosition(position);
        this.radRichTextBox.InsertInline(new Span(" ") { FontSize = 1 });
                   
        hyperlinkEnd.Parent.Children.Remove(hyperlinkEnd);
        this.radRichTextBox.InsertInline(hyperlinkEnd);
    }
}

Hope this information is useful.

Regards,
Tanya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Kamran
Top achievements
Rank 3
Iron
Iron
Veteran
commented on 11 May 2022, 03:34 PM

Thanks Tanya,

As you explained RTF behavior, I handled this situation by traversing html document and removed empty hyperlinks before converting to rtf. This worked for me.

From developer perspective we can understand html and rtf/MS word behaviors, but consumer always expect same result from both content types. 

 

Tags
RichTextBox
Asked by
Kamran
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Tanya
Telerik team
Share this question
or