RadRichTextBox: Issue with extra line breaking on DocumentFragment inserting

1 Answer 75 Views
RichTextBox
Ihor
Top achievements
Rank 1
Ihor asked on 06 Sep 2023, 03:46 PM

Hello. 

I am using custom field extends on CodeBasedField. This field fetch some data form database or prompt window on request. Sometimes its simple string, some time its rft string. I face with problem on handling rft strings. Here is how its looks like in "Code" mode:

Here is how I handle result value of field:

protected override DocumentFragment GetResultFragment()
    {
        var text = "";
        /* 
        text = fetch string handle here
        */
        if (!text.StartsWith(@"{\rtf"))
        {
            var document = new RtfFormatProvider().Import(text);
            return new DocumentFragment(document);
        }
        return CreateFragmentFromText(text);
    }

And if 'text' is rtf the "Result" mode is this:

You can see that extra line breaking there.

I am sure that my rtf have no extra line breaking or paragraphs. As well as "SpacingAfter" parameter for paragraph is 0.

The reason of it is how code handle Paragraphs.

Here is some code from Telerik lib:

How fragment created from document:

How Fragment inserted in RadDocument:

The code inserts "line break" after Paragraph if its single in fragment and if "IsLastParagraphClosed" is true. I cant change value of "IsLastParagraphClosed" because its setter is internal. 

Same problem if my rtf has more then one Paragraph. In that case it use another flow in InsertFragmentInternal method but the result is same - extra line breaking after Field.

 

I found workaround to skip this line breaking, create fragment not from RadDocument but from Selection:

protected override DocumentFragment GetResultFragment()
    {
        var text = "";
        /* 
        text = fetch string handle here
        */
        if (!text.StartsWith(@"{\rtf"))
        {
            var document = new RtfFormatProvider().Import(text);
            document.Selection.SelectAll();
            return new DocumentFragment(document.Selection);
        }
        return CreateFragmentFromText(text);
    }

But in this case its not reproduce full text formatting (e.g. TextAlingment) for my rtf.

Please help me to find proper workaround or fix this issue.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 11 Sep 2023, 07:03 AM

Hi Ihor,

I have tested this with the latest version I was not able to reproduce it. There is no additional paragraph on my side. Are you using the latest version as well? As such cases are very specific I would recommend attaching a sample project where this can be reproduced. This will allow us to properly investigate and determine what is causing this. 

In addition, you can use the following approach to remove the last paragraph in this case:

protected override DocumentFragment GetResultFragment()
{
    var provider = new RtfFormatProvider();
    var document = provider.Import(File.ReadAllText(@"..\..\document.rtf"));

    var par = document.EnumerateChildrenOfType<Paragraph>().LastOrDefault();
    if (par != null && par.Inlines.Count == 0)
    {
        var section = par.Parent as Section;
        section.Blocks.Remove(par);
    }

    return new DocumentFragment(document);
}

Let me know if I can assist you further.

Regards,
Dimitar
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.

Ihor
Top achievements
Rank 1
commented on 15 Sep 2023, 12:02 PM | edited

Hi, Dimitar.

Thank you for your answer!

In attachment to this message you can find simple example project with issue I've described that can be easy reproduced.

I've also recorded small video where you can find how to use my example program and how to reproduce the issue.

https://youtu.be/uQDn_Iar-yY

I am using the latest telerik libs in this example.

How it works. Main view is the RadRichTextBox with 2 buttons. You can insert custom CodeBasedField by pressing "Insert CustomField" button. When you press "Preview" it will force all custom fields to fetch their "ResultFragment" by creating Prompt Window (simple rtf editor).  The result fragment will be the rich text that user entered in prompt window. Once all prompt windows will be closed, the main rich text box will be switched to "Result" mode. And you can see that extra line breaking after each custom field.

Pleas help me with this issue.

Dimitar
Telerik team
commented on 18 Sep 2023, 01:33 PM

Hi Ihor, 

Thank you for sharing the project. I was able to reproduce this. I have logged the issue on your behalf. You can track its progress, subscribe to status changes, and add your comment to it here: RadRichTextBox: Creating a new document fragment from an imported document adds an empty paragraph at the end. I have updated your Telerik points for this report.

As a workaround, I can suggest creating the fragment from the selection. Here is an example: 

var document = new RtfFormatProvider().Import(text);
document.Selection.SelectAll();

var fragment = new DocumentFragment(document.Selection);
 
return fragment;

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,

Dimitar

 

 

 

Ihor
Top achievements
Rank 1
commented on 18 Sep 2023, 05:12 PM

Hello, Dimitar.

I mentioned this workaround in my original topic (you can find it in last paragraph). I cant use this workaround because it does not keep Text Alignment property of Selected text.

I need to keep full text formatting in my case, so will wait for the complex solution.

Any way, thank you, Dimitar.

Dimitar
Telerik team
commented on 19 Sep 2023, 10:02 AM

Hello Ihor, 

The mistake is mine, I missed this yesterday. Nevertheless, I want to suggest another workaround in this case: 

var document = new RtfFormatProvider().Import(text);

var fragment = new DocumentFragment(document);
var prop = typeof(DocumentFragment).GetProperty("IsLastParagraphClosed");
prop.SetValue(fragment, false, null);

Let me know how this works on your side

Ihor
Top achievements
Rank 1
commented on 19 Sep 2023, 11:21 AM

Hi, Dimitar.

Interesting workaround, but unfortunately "Text Alignment" property for the last paragraph is also ignored in this case.

Dimitar
Telerik team
commented on 20 Sep 2023, 10:02 AM

Hi Ihor, 

Okay, I was able to reproduce this as well. I have logged it for investigation on our feedback portal. You can track its progress, subscribe to status changes, and add your comment to it here: RadRichTextBox: Text alignment is lost when creating a fragment from selection, or the IsLastParagraphClosed property is set to false. I have updated your Telerik points for this report. 

I am afraid that I cannot suggest a workaround for this issue.

I want to apologize for the inconvenience this issue is causing you.

Tags
RichTextBox
Asked by
Ihor
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or